I saw the description of QApplication
in the Qt
documentation, but wasn't that clear.
Can you just simply describe what it mainly does?
Thanks.
The QApplication class manages the GUI application's control flow and main settings. QApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization, finalization, and provides session management.
[static] int QApplication::exec() Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()). It is necessary to call this function to start event handling.
exec() method is used in Qt to start the event loop of your QApplication or dialog boxes.
Join Complete Qt6 C++ GUI & Mobile App Development Course in Udemy https://www.udemy.com/course/qt6-c-gu... What is Qt5 (C++ GUI) Qt 5 is the latest version of Qt. It enables developers to develop applications with intuitive user interfaces for multiple targets, faster than ever before.
The most important thing to know about QApplication is that its exec method runs the event loop, which is basically the piece of software that makes slots and signals work.
To put it simply, if there is no running event loop, the events fired by your GUI components will not be propagated at all, so your UI will just not do anything at all.
A Q*Application
instance is what keeps the Qt application alive, by running its main event loop. It processes all events (mouse, keyboard, refresh), signal-slots, timers, and most of Qt features that require some kind of asynchronism or event handling (GUI, OS, ...).
Without a Q*Application, you basically can only run Qt code sequentially. What I mean by that is: no Qt timers, no OS/user interaction. Which is sometimes wanted, for instance in unit tests.
The main thread event loop starts as soon as you call the Q*Application exec()
method, and blocks until application exits.
Depending on your application type, you will want a different flavor of that class to avoid pulling unnecessary dependencies:
QCoreApplication
QGuiApplication
QApplication
You typically create the Q*Application first, initialize the "static" part of your application as needed, and then call qApp->exec()
to run the main event loop. That function will block until your application closes.
int main(int argc, char *argv[]) {
// Q[Core|Gui|)Application
QCoreApplication app(argc, argv);
// Load critical settings i.e. minimal config files, GUI, etc.
// Remember no events, timers, and signals slots will work at that stage
QSettings settings(...);
// Start the QApplication
// Will return once the application closes (forced, or by user)
return app.exec();
}
Only one Q*Application
should exist in your application. You can control it either with its local variable, or the global qApp
pointer.
To copy the documentation of QApplication
class:
QApplication's main areas of responsibility are:
- It initializes the application with the user's desktop settings such as palette(), font() and doubleClickInterval(). It keeps track of these properties in case the user changes the desktop globally, for example through some kind of control panel.
- It performs event handling, meaning that it receives events from the underlying window system and dispatches them to the relevant widgets. By using sendEvent() and postEvent() you can send your own events to widgets.
- It parses common command line arguments and sets its internal state accordingly. See the constructor documentation below for more details.
- It defines the application's look and feel, which is encapsulated in a QStyle object. This can be changed at runtime with setStyle().
- It specifies how the application is to allocate colors. See setColorSpec() for details.
- It provides localization of strings that are visible to the user via translate().
- It provides some magical objects like the desktop() and the clipboard().
- It knows about the application's windows. You can ask which widget is at a certain position using widgetAt(), get a list of topLevelWidgets() and closeAllWindows(), etc.
- It manages the application's mouse cursor handling, see setOverrideCursor()
One of the methods that you can override is QApplication::event
, which gives you total control over how all events are processed in your application. That can be done either by inheriting from it, or using installEventFilter
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With