开发者

Qt - What is QApplication, simply?

I saw the description of QApplication in the Qt documentation, but wasn't that clear.

Can you just simply describe开发者_Python百科 what it mainly does?

Thanks.


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.


Simply put

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.

Choosing the right Q*Application

Depending on your application type, you will want a different flavor of that class to avoid pulling unnecessary dependencies:

  • headless application: QCoreApplication
  • Qt Quick based graphical application: QGuiApplication
  • Qt Widgets based graphical application: 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.

Detailed responsabilities

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.


It's a placeholder for all the application-level global state that exists in a non-OO language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜