QApplication Class

The QApplication class manages the GUI application's control flow and main settings. More...

Header: #include <QApplication>
qmake: QT += widgets
Inherits: QGuiApplication

Properties

Public Functions

QString styleSheet() const

Public Slots

bool autoSipEnabled() const
void setAutoSipEnabled(const bool enabled)
void setStyleSheet(const QString &sheet)

Static Public Members

Macros

Detailed Description

QApplication specializes QGuiApplication with some functionality needed for QWidget-based applications. It handles widget specific initialization, finalization.

For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.

Some GUI applications provide a special batch mode ie. provide command line arguments for executing tasks without manual intervention. In such non-GUI mode, it is often sufficient to instantiate a plain QCoreApplication to avoid unnecessarily initializing resources needed for a graphical user interface. The following example shows how to dynamically create an appropriate type of application instance:

 QCoreApplication* createApplication(int &argc, char *argv[])
 {
     for (int i = 1; i < argc; ++i) {
         if (!qstrcmp(argv[i], "-no-gui"))
             return new QCoreApplication(argc, argv);
     }
     return new QApplication(argc, argv);
 }

 int main(int argc, char* argv[])
 {
     QScopedPointer<QCoreApplication> app(createApplication(argc, argv));

     if (qobject_cast<QApplication *>(app.data())) {
        // start GUI version...
     } else {
        // start non-GUI version...
     }

     return app->exec();
 }

The QApplication object is accessible through the instance() function that returns a pointer equivalent to the global qApp pointer.

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 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()

Since the QApplication object does so much initialization, it must be created before any other objects related to the user interface are created. QApplication also deals with common command line arguments. Hence, it is usually a good idea to create it before any interpretation or modification of argv is done in the application itself.

Groups of functions
System settingsdesktopSettingsAware(), setDesktopSettingsAware(), cursorFlashTime(), setCursorFlashTime(), doubleClickInterval(), setDoubleClickInterval(), setKeyboardInputInterval(), wheelScrollLines(), setWheelScrollLines(), palette(), setPalette(), font(), setFont(), fontMetrics().
Event handlingexec(), processEvents(), exit(), quit(). sendEvent(), postEvent(), sendPostedEvents(), removePostedEvents(), hasPendingEvents(), notify().
GUI Stylesstyle(), setStyle().
Text handlinginstallTranslator(), removeTranslator() translate().
WidgetsallWidgets(), topLevelWidgets(), desktop(), activePopupWidget(), activeModalWidget(), clipboard(), focusWidget(), activeWindow(), widgetAt().
Advanced cursor handlingoverrideCursor(), setOverrideCursor(), restoreOverrideCursor().
MiscellaneouscloseAllWindows(), startingUp(), closingDown().

See also QCoreApplication, QAbstractEventDispatcher, QEventLoop, and QSettings.

Property Documentation

autoSipEnabled : bool

toggles automatic SIP (software input panel) visibility

Set this property to true to automatically display the SIP when entering widgets that accept keyboard input. This property only affects widgets with the WA_InputMethodEnabled attribute set, and is typically used to launch a virtual keyboard on devices which have very few or no keys.

The property only has an effect on platforms that use software input panels.

The default is platform dependent.

This property was introduced in Qt 4.5.

Access functions:

bool autoSipEnabled() const
void setAutoSipEnabled(const bool enabled)

cursorFlashTime : int

This property holds the text cursor's flash (blink) time in milliseconds

The flash time is the time required to display, invert and restore the caret display. Usually the text cursor is displayed for half the cursor flash time, then hidden for the same amount of time, but this may vary.

The default value on X11 is 1000 milliseconds. On Windows, the Control Panel value is used and setting this property sets the cursor flash time for all applications.

We recommend that widgets do not cache this value as it may change at any time if the user changes the global desktop settings.

Note: This property may hold a negative value, for instance if cursor blinking is disabled.

Access functions:

int cursorFlashTime()
void setCursorFlashTime(int)

doubleClickInterval : int

This property holds the time limit in milliseconds that distinguishes a double click from two consecutive mouse clicks

The default value on X11 is 400 milliseconds. On Windows and Mac OS, the operating system's value is used.

Access functions:

int doubleClickInterval()
void setDoubleClickInterval(int)

keyboardInputInterval : int

This property holds the time limit in milliseconds that distinguishes a key press from two consecutive key presses

The default value on X11 is 400 milliseconds. On Windows and Mac OS, the operating system's value is used.

This property was introduced in Qt 4.2.

Access functions:

int keyboardInputInterval()
void setKeyboardInputInterval(int)

startDragDistance : int

If you support drag and drop in your application, and want to start a drag and drop operation after the user has moved the cursor a certain distance with a button held down, you should use this property's value as the minimum distance required.

For example, if the mouse position of the click is stored in startPos and the current position (e.g. in the mouse move event) is currentPos, you can find out if a drag should be started with code like this:

 if ((startPos - currentPos).manhattanLength() >=
         QApplication::startDragDistance())
     startTheDrag();

Qt uses this value internally, e.g. in QFileDialog.

The default value (if the platform doesn't provide a different default) is 10 pixels.

Access functions:

int startDragDistance()
void setStartDragDistance(int l)

See also startDragTime(), QPoint::manhattanLength(), and Drag and Drop.

startDragTime : int

This property holds the time in milliseconds that a mouse button must be held down before a drag and drop operation will begin

If you support drag and drop in your application, and want to start a drag and drop operation after the user has held down a mouse button for a certain amount of time, you should use this property's value as the delay.

Qt also uses this delay internally, e.g. in QTextEdit and QLineEdit, for starting a drag.

The default value is 500 ms.

Access functions:

int startDragTime()
void setStartDragTime(int ms)

See also startDragDistance() and Drag and Drop.

styleSheet : QString

This property holds the application style sheet

By default, this property returns an empty string unless the user specifies the -stylesheet option on the command line when running the application.

This property was introduced in Qt 4.2.

Access functions:

QString styleSheet() const
void setStyleSheet(const QString &sheet)

See also QWidget::setStyle() and Qt Style Sheets.

wheelScrollLines : int

This property holds the number of lines to scroll a widget, when the mouse wheel is rotated.

If the value exceeds the widget's number of visible lines, the widget should interpret the scroll operation as a single page up or page down. If the widget is an item view class, then the result of scrolling one line depends on the setting of the widget's scroll mode. Scroll one line can mean scroll one item or scroll one pixel.

By default, this property has a value of 3.

Access functions:

int wheelScrollLines()
void setWheelScrollLines(int)

See also QStyleHints::wheelScrollLines().

windowIcon : QIcon

This property holds the default window icon

Access functions:

QIcon windowIcon()
void setWindowIcon(const QIcon &icon)

See also QWidget::setWindowIcon() and Setting the Application Icon.

Macro Documentation

qApp

A global pointer referring to the unique application object. It is equivalent to QCoreApplication::instance(), but cast as a QApplication pointer, so only valid when the unique application object is a QApplication.

See also QCoreApplication::instance() and qGuiApp.