Reputation: 21
Class Window (QMainWindow):
Class Window (QApplication):
I tried making a window with either of them noticing no changes...so
Upvotes: 0
Views: 2289
Reputation: 48231
You're practically asking something like this:
what's the difference between subclassing
list
and subclassingint
?
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:
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:
Upvotes: 5