SawMEr
SawMEr

Reputation: 21

Difference between subclassing QMainWindow and QApplication

Class Window (QMainWindow):
Class Window (QApplication):

I tried making a window with either of them noticing no changes...so

Upvotes: 0

Views: 2289

Answers (1)

musicamante
musicamante

Reputation: 48231

You're practically asking something like this:

what's the difference between subclassing list and subclassing int?

While both of them inherit from the same class (QObject in your case, python object for list and int), they do radically different things.

QApplication inherits from QGuiApplication (which, in turn, inherits from QCoreApplication and finally QObject).
A Qt application is the one responsible of running and managing the Qt event loop, and its creation is mandatory to create and interact with many Qt aspects. Most importantly, a QApplication is always required for Qt widgets (windows, buttons, etc), so that they are correctly shown and provide full user interaction.

When you start the application (by running its exec()), you enter its event loop. This means that it will wait for any event coming from the code, the user, or the system, and will eventually react accordingly. That's the concept of an event loop: it will just silently idle, waiting until "something" happens.

For example, if you call show() on a QDialog, Qt will create a show event, which in turn will be "sent" to the operating system, telling it that a new "window" is going to be shown; the system will create a window and the handler for that window will be known to Qt, which will create a "paint event" that will be received by the dialog's paintEvent: in that method the window will be actually "painted" and it will be finally shown to the user. (Note, this is a huge oversimplification: things are actually more complex than this, but that's not the scope of this answer).

QMainWindow inherits from QWidget, the base class for all objects that represent a GUI element in Qt.
Simply speaking, on its own it does almost nothing. It only reacts to the events the Qt application sends: the aforementioned paint events, but also keyboard/mouse events or system requests. If a paintEvent is sent to it, it will "paint" the window contents. If a mouse button click happens on it, it will try to send it to the widget that exists at the coordinates of that event, etc.

As you can see, those classes do very different things.
In conclusion: there's a huge difference between subclassing a QMainWindow or a QApplication... As there's none.
It all depends on your needs, since the use of those classes is very different, as it is between list and int.

Subclassing is generally done only when you need to extend or modify the functionalities of an existing class. If you need to extend or modify the behavior of a Qt main window widget, you'll subclass QMainWindow. If you need to extend or modify the behavior of a Qt application, you'll subclass Q[Core|Gui]Application.

Possible scenarios:

  • I want to create a main window class that, when instantiated, it already has a menubar with a "file" submenu and some widgets, so that I can create more main windows and all of them are already set up with that menu: subclass QMainWindow
  • I need an application that manages multiple main windows, and I want to keep track of them using a common interface to interact with them (when a window closes, open the other, etc): subclass QApplication
  • I only need one main window, but I have to override lots of its methods and "monkey patching" would be a mess: subclass QMainWindow
  • I have to filter application-wide events and do something with them (for example, there are multiple windows and dialogs, but I don't want to quit the application if all of them are closed as some might reappear after some time or upon some user interaction): subclass QApplication

Note that all the cases listed above could theoretically be achieved without subclassing and only using anonymous functions, but that would make your code less tidy and, most importantly, less readable and debuggable.

Finally:

  1. consider that in most cases subclassing a Qt application is not necessary;
  2. both classes inherit from QObject wich is the basis for most of Qt classes: it offers support for the signal/slot mechanism, can interact to Qt events or with an application (if it exists, not all QObject descendant require a Qt application: a Qt application is a QObject subclass itself);
  3. you should carefully study how python classes work and, generally, what classes and instances are;

Upvotes: 5

Related Questions