QApplication Class

QApplication 管理 GUI 应用程序的控制流和主要设置. 更多...

头文件: #include <QApplication>
qmake: QT += widgets
基类: QGuiApplication

属性

公有函数

QApplication(int &argc, char **argv)
virtual ~QApplication()
bool notify(QObject *receiver, QEvent *e)
QString styleSheet() const

公有槽函数

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

信号

void focusChanged(QWidget *old, QWidget *now)

静态公有成员

QWidget *activeModalWidget()
QWidget *activePopupWidget()
QWidget *activeWindow()
void alert(QWidget *widget, int msec = 0)
QWidgetList allWidgets()
void beep()
int cursorFlashTime()
QDesktopWidget *desktop()
int doubleClickInterval()
int exec()
QWidget *focusWidget()
QFont font()
QFont font(const QWidget *widget)
QFont font(const char *className)
QFontMetrics fontMetrics()
QSize globalStrut()
bool isEffectEnabled(Qt::UIEffect effect)
int keyboardInputInterval()
Qt::NavigationMode navigationMode()
QPalette palette(const QWidget *widget)
QPalette palette(const char *className)
void setActiveWindow(QWidget *active)
void setCursorFlashTime(int)
void setDoubleClickInterval(int)
void setEffectEnabled(Qt::UIEffect effect, bool enable = true)
void setFont(const QFont &font, const char *className = Q_NULLPTR)
void setGlobalStrut(const QSize &)
void setKeyboardInputInterval(int)
void setNavigationMode(Qt::NavigationMode mode)
void setPalette(const QPalette &palette, const char *className = Q_NULLPTR)
void setStartDragDistance(int l)
void setStartDragTime(int ms)
void setStyle(QStyle *style)
QStyle *setStyle(const QString &style)
void setWheelScrollLines(int)
void setWindowIcon(const QIcon &icon)
int startDragDistance()
int startDragTime()
QStyle *style()
QWidget *topLevelAt(const QPoint &point)
QWidget *topLevelAt(int x, int y)
QWidgetList topLevelWidgets()
int wheelScrollLines()
QWidget *widgetAt(const QPoint &point)
QWidget *widgetAt(int x, int y)
QIcon windowIcon()

受保护的函数

bool event(QEvent *e)

Macros

详细描述

QApplication 管理 GUI 应用程序的控制流和主要设置.

QApplication 派生自 QGuiApplication, 提供基于 QWidget 的应用程序所需的一些功能. 它处理小部件特定的初始化和完成.

对于任何使用 Qt 的 GUI 应用程序, 无论有多少个窗口, 都只有一个 QApplication 对象. 对于非基于 QWidget 的 Qt 应用程序, 使用 QGuiApplication 代替, 因为它不依赖于 QtWidgets 库.

一些 GUI 应用程序提供特殊的批处理模式, 即, 提供用于执行任务的命令行参数, 无需手动干预. 在非 GUI 模式下, 实例化一个普通的 QCoreApplication 通常就足够了, 以避免不必要地初始化图形用户界面所需的资源. 以下示例展示了如何动态创建适当类型的应用程序实例:


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

QApplication 对象可通过instance() 函数访问, 这个函数返回一个与全局变量 qApp 等效的指针.

QApplication的主要职责范围是:

  • 它使用用户的桌面设置(例如palette(), font() 和 doubleClickInterval()) 初始化应用程序. 它会跟踪这些属性, 以防用户全局更改桌面(例如通过某种控制面板).
  • 它执行事件处理, 这意味着它从底层窗口系统接收事件并将它们分派到相关的widget. 通过使用 sendEvent() 和 postEvent(), 你可以将自己的事件发送到widget.
  • 它解析常见的命令行参数并相应地设置其内部状态. 详见constructor documentation.
  • 它定义了应用程序的外观, 并封装在 QStyle 对象中. 这可以在运行时使用 setStyle() 进行更改.
  • It specifies how the application is to allocate colors. See setColorSpec() for details.
  • 它通过translate() 提供用户可见的字符串的本地化.
  • 它提供了一些特殊的对象, 例如 desktop() 和 clipboard().
  • 它知道应用程序的窗口. 你可以使用 widgetAt() 询问哪个widget位于某个位置, 获取 topLevelWidgets() 和 closeAllWindows() 的列表等.
  • 它管理应用程序的鼠标光标处理, 参见 setOverrideCursor()

由于 QApplication 对象进行了如此多的初始化, 因此必须在创建与用户界面相关的任何其他对象之前创建它. QApplication 还处理常见的命令行参数. 因此, 在应用程序本身对 argv 进行任何解释或修改之前创建它通常是一个好主意.

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().
Color usagecolorSpec(), setColorSpec().
Text handlinginstallTranslator(), removeTranslator() translate().
WidgetsallWidgets(), topLevelWidgets(), desktop(), activePopupWidget(), activeModalWidget(), clipboard(), focusWidget(), activeWindow(), widgetAt().
Advanced cursor handlingoverrideCursor(), setOverrideCursor(), restoreOverrideCursor().
MiscellaneouscloseAllWindows(), startingUp(), closingDown().

参见 QCoreApplication, QAbstractEventDispatcher, QEventLoop, and QSettings.

属性

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.

访问函数:

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.

访问函数:

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.

访问函数:

int doubleClickInterval()
void setDoubleClickInterval(int)

globalStrut : QSize

This property holds the minimum size that any GUI element that the user can interact with should have

For example, no button should be resized to be smaller than the global strut size. The strut size should be considered when reimplementing GUI controls that may be used on touch-screens or similar I/O devices.

Example:


  QSize MyWidget::sizeHint() const
  {
      return QSize(80, 25).expandedTo(QApplication::globalStrut());
  }

By default, this property contains a QSize object with zero width and height.

访问函数:

QSize globalStrut()
void setGlobalStrut(const QSize &)

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.

访问函数:

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:


  int main(int argc, char *argv[])
  {
      QApplication::setDesktopSettingsAware(false);
      QApplication app(argc, argv);
      ...
      return app.exec();
  }

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

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

访问函数:

int startDragDistance()
void setStartDragDistance(int l)

参见 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.

访问函数:

int startDragTime()
void setStartDragTime(int ms)

参见 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.

访问函数:

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

参见 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.

访问函数:

int wheelScrollLines()
void setWheelScrollLines(int)

参见 QStyleHints::wheelScrollLines().

windowIcon : QIcon

This property holds the default window icon

访问函数:

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

参见 QWidget::setWindowIcon() and Setting the Application Icon.

成员函数

QApplication::QApplication(int &argc, char **argv)

Initializes the window system and constructs an application object with argc command line arguments in argv.

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

The global qApp pointer refers to this application object. Only one application object should be created.

This application object must be constructed before any paint devices (including widgets, pixmaps, bitmaps etc.).

Note: argc and argv might be changed as Qt removes command line arguments that it recognizes.

All Qt programs automatically support the following command line options:

  • -style= style, sets the application GUI style. Possible values depend on your system configuration. If you compiled Qt with additional styles or have additional styles as plugins these will be available to the -style command line option. You can also set the style for all Qt applications by setting the QT_STYLE_OVERRIDE environment variable.
  • -style style, is the same as listed above.
  • -stylesheet= stylesheet, sets the application styleSheet. The value must be a path to a file that contains the Style Sheet.

    Note: Relative URLs in the Style Sheet file are relative to the Style Sheet file's path.

  • -stylesheet stylesheet, is the same as listed above.
  • -widgetcount, prints debug message at the end about number of widgets left undestroyed and maximum number of widgets existed at the same time
  • -reverse, sets the application's layout direction to Qt::RightToLeft
  • -qmljsdebugger=, activates the QML/JS debugger with a specified port. The value must be of format port:1234[,block], where block is optional and will make the application wait until a debugger connects to it.

参见 QCoreApplication::arguments().

[virtual] QApplication::~QApplication()

Cleans up any window system resources that were allocated by this application. Sets the global variable qApp to 0.

[static slot] void QApplication::aboutQt()

Displays a simple message box about Qt. The message includes the version number of Qt being used by the application.

This is useful for inclusion in the Help menu of an application, as shown in the Menus example.

This function is a convenience slot for QMessageBox::aboutQt().

[static] QWidget *QApplication::activeModalWidget()

Returns the active modal widget.

A modal widget is a special top-level widget which is a subclass of QDialog that specifies the modal parameter of the constructor as true. A modal widget must be closed before the user can continue with other parts of the program.

Modal widgets are organized in a stack. This function returns the active modal widget at the top of the stack.

参见 activePopupWidget() and topLevelWidgets().

[static] QWidget *QApplication::activePopupWidget()

Returns the active popup widget.

A popup widget is a special top-level widget that sets the Qt::WType_Popup widget flag, e.g. the QMenu widget. When the application opens a popup widget, all events are sent to the popup. Normal widgets and modal widgets cannot be accessed before the popup widget is closed.

Only other popup widgets may be opened when a popup widget is shown. The popup widgets are organized in a stack. This function returns the active popup widget at the top of the stack.

参见 activeModalWidget() and topLevelWidgets().

[static] QWidget *QApplication::activeWindow()

Returns the application top-level window that has the keyboard input focus, or 0 if no application window has the focus. There might be an activeWindow() even if there is no focusWidget(), for example if no widget in that window accepts key events.

参见 setActiveWindow(), QWidget::setFocus(), QWidget::hasFocus(), and focusWidget().

[static] void QApplication::alert(QWidget *widget, int msec = 0)

Causes an alert to be shown for widget if the window is not the active window. The alert is shown for msec miliseconds. If msec is zero (the default), then the alert is shown indefinitely until the window becomes active again.

Currently this function does nothing on Qt for Embedded Linux.

On macOS, this works more at the application level and will cause the application icon to bounce in the dock.

On Windows, this causes the window's taskbar entry to flash for a time. If msec is zero, the flashing will stop and the taskbar entry will turn a different color (currently orange).

On X11, this will cause the window to be marked as "demands attention", the window must not be hidden (i.e. not have hide() called on it, but be visible in some sort of way) in order for this to work.

This function was introduced in Qt 4.3.

[static] QWidgetList QApplication::allWidgets()

Returns a list of all the widgets in the application.

The list is empty (QList::isEmpty()) if there are no widgets.

Note: Some of the widgets may be hidden.

Example:


  void updateAllWidgets()
  {
      foreach (QWidget *widget, QApplication::allWidgets())
          widget->update();
  }

参见 topLevelWidgets() and QWidget::isVisible().

[static] void QApplication::beep()

Sounds the bell, using the default volume and sound. The function is not available in Qt for Embedded Linux.

[static slot] void QApplication::closeAllWindows()

Closes all top-level windows.

This function is particularly useful for applications with many top-level windows. It could, for example, be connected to a Exit entry in the File menu:


      const QIcon exitIcon = QIcon::fromTheme("application-exit");
      QAction *exitAct = fileMenu->addAction(exitIcon, tr("E&xit"), qApp, &QApplication::closeAllWindows);
      exitAct->setShortcuts(QKeySequence::Quit);
      exitAct->setStatusTip(tr("Exit the application"));
      fileMenu->addAction(exitAct);

The windows are closed in random order, until one window does not accept the close event. The application quits when the last window was successfully closed; this can be turned off by setting quitOnLastWindowClosed to false.

参见 quitOnLastWindowClosed, lastWindowClosed(), QWidget::close(), QWidget::closeEvent(), lastWindowClosed(), QCoreApplication::quit(), topLevelWidgets(), and QWidget::isWindow().

[static] QDesktopWidget *QApplication::desktop()

Returns the desktop widget (also called the root window).

The desktop may be composed of multiple screens, so it would be incorrect, for example, to attempt to center some widget in the desktop's geometry. QDesktopWidget has various functions for obtaining useful geometries upon the desktop, such as QDesktopWidget::screenGeometry() and QDesktopWidget::availableGeometry().

On X11, it is also possible to draw on the desktop.

[protected] bool QApplication::event(QEvent *e)

[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. The main event loop receives events from the window system and dispatches these to the application widgets.

Generally, no user interaction can take place before calling exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widgets call exec() to start a local event loop.

To make your application perform idle processing, i.e., executing a special function whenever there are no pending events, use a QTimer with 0 timeout. More advanced idle processing schemes can be achieved using processEvents().

We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication::exec() call.

参见 quitOnLastWindowClosed, QCoreApplication::quit(), QCoreApplication::exit(), QCoreApplication::processEvents(), and QCoreApplication::exec().

[signal] void QApplication::focusChanged(QWidget *old, QWidget *now)

This signal is emitted when the widget that has keyboard focus changed from old to now, i.e., because the user pressed the tab-key, clicked into a widget or changed the active window. Both old and now can be the null-pointer.

The signal is emitted after both widget have been notified about the change through QFocusEvent.

This function was introduced in Qt 4.1.

参见 QWidget::setFocus(), QWidget::clearFocus(), and Qt::FocusReason.

[static] QWidget *QApplication::focusWidget()

Returns the application widget that has the keyboard input focus, or 0 if no widget in this application has the focus.

参见 QWidget::setFocus(), QWidget::hasFocus(), activeWindow(), and focusChanged().

[static] QFont QApplication::font()

Returns the default application font.

参见 setFont(), fontMetrics(), and QWidget::font().

[static] QFont QApplication::font(const QWidget *widget)

This is an overloaded function.

Returns the default font for the widget.

参见 fontMetrics() and QWidget::setFont().

[static] QFont QApplication::font(const char *className)

This is an overloaded function.

Returns the font for widgets of the given className.

参见 setFont() and QWidget::font().

[static] QFontMetrics QApplication::fontMetrics()

Returns display (screen) font metrics for the application font.

参见 font(), setFont(), QWidget::fontMetrics(), and QPainter::fontMetrics().

[static] bool QApplication::isEffectEnabled(Qt::UIEffect effect)

Returns true if effect is enabled; otherwise returns false.

By default, Qt will try to use the desktop settings. To prevent this, call setDesktopSettingsAware(false).

Note: All effects are disabled on screens running at less than 16-bit color depth.

参见 setEffectEnabled() and Qt::UIEffect.

Returns what kind of focus navigation Qt is using.

This feature is available in Qt for Embedded Linux only.

This function was introduced in Qt 4.6.

参见 setNavigationMode() and keypadNavigationEnabled().

bool QApplication::notify(QObject *receiver, QEvent *e)

[static] QPalette QApplication::palette(const QWidget *widget)

This is an overloaded function.

If a widget is passed, the default palette for the widget's class is returned. This may or may not be the application palette. In most cases there is no special palette for certain types of widgets, but one notable exception is the popup menu under Windows, if the user has defined a special background color for menus in the display settings.

参见 setPalette() and QWidget::palette().

[static] QPalette QApplication::palette(const char *className)

This is an overloaded function.

Returns the palette for widgets of the given className.

参见 setPalette() and QWidget::palette().

[static] void QApplication::setActiveWindow(QWidget *active)

Sets the active window to the active widget in response to a system event. The function is called from the platform specific event handlers.

Warning: This function does not set the keyboard focus to the active widget. Call QWidget::activateWindow() instead.

It sets the activeWindow() and focusWidget() attributes and sends proper WindowActivate/WindowDeactivate and FocusIn/FocusOut events to all appropriate widgets. The window will then be painted in active state (e.g. cursors in line edits will blink), and it will have tool tips enabled.

参见 activeWindow() and QWidget::activateWindow().

[static] void QApplication::setEffectEnabled(Qt::UIEffect effect, bool enable = true)

Enables the UI effect effect if enable is true, otherwise the effect will not be used.

Note: All effects are disabled on screens running at less than 16-bit color depth.

参见 isEffectEnabled(), Qt::UIEffect, and setDesktopSettingsAware().

[static] void QApplication::setFont(const QFont &font, const char *className = Q_NULLPTR)

Changes the default application font to font. If className is passed, the change applies only to classes that inherit className (as reported by QObject::inherits()).

On application start-up, the default font depends on the window system. It can vary depending on both the window system version and the locale. This function lets you override the default font; but overriding may be a bad idea because, for example, some locales need extra large fonts to support their special characters.

Warning: Do not use this function in conjunction with Qt Style Sheets. The font of an application can be customized using the "font" style sheet property. To set a bold font for all QPushButtons, set the application styleSheet() as "QPushButton { font: bold }"

参见 font(), fontMetrics(), and QWidget::setFont().

[static] void QApplication::setNavigationMode(Qt::NavigationMode mode)

Sets the kind of focus navigation Qt should use to mode.

This feature is available in Qt for Embedded Linux only.

This function was introduced in Qt 4.6.

参见 navigationMode() and keypadNavigationEnabled().

[static] void QApplication::setPalette(const QPalette &palette, const char *className = Q_NULLPTR)

Changes the default application palette to palette.

If className is passed, the change applies only to widgets that inherit className (as reported by QObject::inherits()). If className is left 0, the change affects all widgets, thus overriding any previously set class specific palettes.

The palette may be changed according to the current GUI style in QStyle::polish().

Warning: Do not use this function in conjunction with Qt Style Sheets. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color".

Note: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for the Windows XP, Windows Vista, and macOS styles.

参见 QWidget::setPalette(), palette(), and QStyle::polish().

[static] void QApplication::setStyle(QStyle *style)

Sets the application's GUI style to style. Ownership of the style object is transferred to QApplication, so QApplication will delete the style object on application exit or when a new style is set and the old style is still the parent of the application object.

Example usage:


  QApplication::setStyle(QStyleFactory::create("Fusion"));

When switching application styles, the color palette is set back to the initial colors or the system defaults. This is necessary since certain styles have to adapt the color palette to be fully style-guide compliant.

Setting the style before a palette has been set, i.e., before creating QApplication, will cause the application to use QStyle::standardPalette() for the palette.

Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.

参见 style(), QStyle, setPalette(), and desktopSettingsAware().

[static] QStyle *QApplication::setStyle(const QString &style)

This is an overloaded function.

Requests a QStyle object for style from the QStyleFactory.

The string must be one of the QStyleFactory::keys(), typically one of "windows", "fusion", "windowsxp", or "macintosh". Style names are case insensitive.

Returns 0 if an unknown style is passed, otherwise the QStyle object returned is set as the application's GUI style.

Warning: To ensure that the application's style is set correctly, it is best to call this function before the QApplication constructor, if possible.

[static] QStyle *QApplication::style()

Returns the application's style object.

参见 setStyle() and QStyle.

[static] QWidget *QApplication::topLevelAt(const QPoint &point)

Returns the top-level widget at the given point; returns 0 if there is no such widget.

[static] QWidget *QApplication::topLevelAt(int x, int y)

This is an overloaded function.

Returns the top-level widget at the point (x, y); returns 0 if there is no such widget.

[static] QWidgetList QApplication::topLevelWidgets()

Returns a list of the top-level widgets (windows) in the application.

Note: Some of the top-level widgets may be hidden, for example a tooltip if no tooltip is currently shown.

Example:


  void showAllHiddenTopLevelWidgets()
  {
      foreach (QWidget *widget, QApplication::topLevelWidgets()) {
          if (widget->isHidden())
              widget->show();
      }
  }

参见 allWidgets(), QWidget::isWindow(), and QWidget::isHidden().

[static] QWidget *QApplication::widgetAt(const QPoint &point)

Returns the widget at global screen position point, or 0 if there is no Qt widget there.

This function can be slow.

参见 QCursor::pos(), QWidget::grabMouse(), and QWidget::grabKeyboard().

[static] QWidget *QApplication::widgetAt(int x, int y)

This is an overloaded function.

Returns the widget at global screen position (x, y), or 0 if there is no Qt widget there.

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.

参见 QCoreApplication::instance() and qGuiApp.