QApplication 类

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

头: #include <QApplication>
qmake: QT += widgets
继承: QGuiApplication

特性

公共函数

QApplication (int & argc , char ** argv )
virtual ~QApplication ()
QString styleSheet () const

重实现公共函数

virtual bool notify (QObject * receiver , QEvent * e )

公共槽

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

重实现保护函数

virtual bool event (QEvent * e )

qApp

详细描述

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

QApplication 专攻 QGuiApplication 具有一些所需功能对于 QWidget 基应用程序。它处理 Widget 特定初始化、定稿。

对于使用 QT 的任何 GUI 应用程序,准确存在 one QApplication 对象,无论应用程序在任何给定时间是否拥有 0 个、1 个、2 个或更多个窗口。对于非 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。
  • 它剖析常见命令行自变量从而设置其内部状态。见 构造函数文档编制 下文了解更多细节。
  • 它定义应用程序的外观和感觉,封装在 QStyle 对象。这可以改变在运行时采用 setStyle ().
  • 它指定应用程序如何分配颜色。见 setColorSpec() 了解细节。
  • 它提供用户可见的字符串本地化凭借 translate ().
  • 它提供某些魔法对象,像 desktop () 和 clipboard ().
  • 它知道有关应用程序的窗口。可以询问在某个位置是哪个 Widget 使用 widgetAt (),获取列表为 topLevelWidgets () 和 closeAllWindows (),等。
  • 它管理应用程序的鼠标光标处理,见 setOverrideCursor ()

由于 QApplication 对象会做如此多的初始化,它 must 被创建,在创建用户界面相关的任何其它对象之前。 QApplication 还处理常见命令行自变量。因此,创建它通常是一个好主意 before 任何解释或修改对于 argv 的履行在应用程序本身。

函数组
系统设置 desktopSettingsAware (), setDesktopSettingsAware (), cursorFlashTime (), setCursorFlashTime (), doubleClickInterval (), setDoubleClickInterval (), setKeyboardInputInterval (), wheelScrollLines (), setWheelScrollLines (), palette (), setPalette (), font (), setFont (), fontMetrics ().
事件处理 exec (), processEvents (), exit (), quit (). sendEvent (), postEvent (), sendPostedEvents (), removePostedEvents (), hasPendingEvents(), notify ().
GUI 样式 style (), setStyle ().
颜色用法 colorSpec(), setColorSpec().
文本处理 installTranslator (), removeTranslator () translate ().
Widgets allWidgets (), topLevelWidgets (), desktop (), activePopupWidget (), activeModalWidget (), clipboard (), focusWidget (), activeWindow (), widgetAt ().
高级光标处理 overrideCursor (), setOverrideCursor (), restoreOverrideCursor ().
杂项 closeAllWindows (), startingUp (), closingDown ().

另请参阅 QCoreApplication , QAbstractEventDispatcher , QEventLoop ,和 QSettings .

特性文档编制

autoSipEnabled : bool

切换自动 SIP (软件输入面板) 可见性

把此特性设为 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.

此特性仅对使用软件输入面板的平台有影响。

默认从属平台。

该特性在 Qt 4.5 引入。

访问函数:

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

cursorFlashTime : int

此特性保持文本光标的刷新 (眨眼) 时间,以毫秒为单位

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 控制面板 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.

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

访问函数:

int cursorFlashTime ()
void setCursorFlashTime ( int )

doubleClickInterval : int

此特性保持以毫秒为单位的时间限制,用于区分双击和连续 2 次鼠标点击

在 X11 默认值为 400 毫秒。在Windows 和 Mac OS 使用操作系统的值。

访问函数:

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.

范例:

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

默认情况下,此特性包含 QSize object with zero width and height.

访问函数:

QSize globalStrut ()
void setGlobalStrut ( const QSize & )

keyboardInputInterval : int

此特性以毫秒为单位保持 (区分键按下和两次连续键按下的) 时间限制

在 X11 默认值为 400 毫秒。在Windows 和 Mac OS 使用操作系统的值。

该特性在 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 使用此值在内部,如在 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 (),和 拖放 .

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.

默认值为 500 毫秒。

访问函数:

int startDragTime ()
void setStartDragTime (int ms )

另请参阅 startDragDistance () 和 拖放 .

styleSheet : QString

此特性保持应用程序样式表

默认情况下,此特性返回空字符串除非用户指定 -stylesheet 选项在命令行当运行应用程序时。

该特性在 Qt 4.2 引入。

访问函数:

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

另请参阅 QWidget::setStyle () 和 Qt 样式表 .

wheelScrollLines : int

此特性保持 Widget 要卷动的行数,当鼠标滚轮旋转时。

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

此特性保持默认窗口图标

访问函数:

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

另请参阅 QWidget::setWindowIcon () 和 设置应用程序图标 .

成员函数文档编制

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

初始化窗口系统并构造应用程序对象采用 argc 命令行自变量在 argv .

警告: 数据的引用通过 argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc 必须大于 0 且 argv 必须包含至少一有效字符串。

全局 qApp 指针引用此应用程序对象。只应创建一个应用程序对象。

此应用程序对象必须被构造先于任何 描绘设备 (包括小部件、像素图、位图等)。

注意: argc and argv 可能改变当 Qt 移除它识别的命令行自变量时。

所有 Qt 程序自动支持以下命令行选项:

  • -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 环境变量。
  • -style style ,与上文列出的相同。
  • -stylesheet= stylesheet , sets the application styleSheet . The value must be a path to a file that contains the Style Sheet.

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

  • -stylesheet stylesheet ,与上文列出的相同。
  • -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 菜单 范例。

此函数是方便槽对于 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 () 和 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 () 和 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 (),例如:若该窗口的 Widget 不接受键事件。

另请参阅 setActiveWindow (), QWidget::setFocus (), QWidget::hasFocus (),和 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.

该函数在 Qt 4.3 引入。

[static] QWidgetList QApplication:: allWidgets ()

返回应用程序的所有 Widget 的列表。

列表是空的 ( QList::isEmpty ()) 若没有 Widget。

注意: 某些 Widget 可能被隐藏。

范例:

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

另请参阅 topLevelWidgets () 和 QWidget::isVisible ().

[static] void QApplication:: beep ()

响铃声,使用默认音量和声音。函数 not 可用于 Qt for Embedded Linux。

[static slot] void QApplication:: closeAllWindows ()

关闭所有顶层窗口。

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 为 false。

另请参阅 quitOnLastWindowClosed , lastWindowClosed (), QWidget::close (), QWidget::closeEvent (), lastWindowClosed (), QCoreApplication::quit (), topLevelWidgets (),和 QWidget::isWindow ().

[static] QDesktopWidget *QApplication:: desktop ()

返回桌面 Widget (也称根窗口)。

桌面可能由多个屏幕组成,因此它将是不正确的,例如:试图 center some widget in the desktop's geometry. QDesktopWidget has various functions for obtaining useful geometries upon the desktop, such as QDesktopWidget::screenGeometry () 和 QDesktopWidget::availableGeometry ().

在 X11,在桌面上绘制也是可能的。

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

重实现自 QObject::event ().

[static] int QApplication:: exec ()

进入主事件循环并等待,直到 exit () is called, then returns the value that was set to exit () (其为 0 若 exit () 被调用凭借 quit ()).

有必要调用此函数以启动事件处理。主事件循环从窗口系统接收事件,并将其分派给应用程序 Widget。

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 采用 0 超时。更高级空闲处理方案可以达成使用 processEvents ().

推荐把清理代码连接到 aboutToQuit() 信号,而不是将它放入应用程序的 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 (),和 QCoreApplication::exec ().

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

此信号被发射当 Widget 键盘聚焦的改变是从 old to now ,即,由于用户按下 tab 键、点击 Widget,或改变活动窗口。两者 old and now 可以是 null 指针。

信号被发射后于 2 小部件被通知即将改变透过 QFocusEvent .

该函数在 Qt 4.1 引入。

另请参阅 QWidget::setFocus (), QWidget::clearFocus (),和 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 (),和 focusChanged ().

[static] QFont QApplication:: font ()

返回默认的应用程序字体。

另请参阅 setFont (), fontMetrics (),和 QWidget::font ().

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

这是重载函数。

返回默认字体,为 widget .

另请参阅 fontMetrics () 和 QWidget::setFont ().

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

这是重载函数。

返回 Widget 字体为给定 className .

另请参阅 setFont () 和 QWidget::font ().

[static] QFontMetrics QApplication:: fontMetrics ()

返回用于应用程序字体的显示 (屏幕) 字体规格。

另请参阅 font (), setFont (), QWidget::fontMetrics (),和 QPainter::fontMetrics ().

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

返回 true if effect 被启用;否则返回 false .

默认情况下,Qt 试着使用桌面设置。要阻止这,调用 setDesktopSettingsAware (false).

注意: 所有效果被禁用,当运行在低于 16 位颜色深度的屏幕。

另请参阅 setEffectEnabled () 和 Qt::UIEffect .

返回 Qt 使用哪种聚焦导航。

此特征只可用于 Qt for Embedded Linux。

该函数在 Qt 4.6 引入。

另请参阅 setNavigationMode () 和 keypadNavigationEnabled ().

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

重实现自 QCoreApplication::notify ().

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

这是重载函数。

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 () 和 QWidget::palette ().

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

这是重载函数。

返回用于 Widget 的调色板为给定 className .

另请参阅 setPalette () 和 QWidget::palette ().

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

将活动窗口设为 active 小部件以响应系统事件。函数的调用是从平台特定事件处理程序。

警告: 此函数履行 not set the keyboard focus to the active widget. Call QWidget::activateWindow () 代替。

It sets the activeWindow () 和 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 () 和 QWidget::activateWindow ().

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

启用 UI 效果 effect if enable 为 True,否则效果将不会被使用。

注意: 所有效果被禁用,当运行在低于 16 位颜色深度的屏幕。

另请参阅 isEffectEnabled (), Qt::UIEffect ,和 setDesktopSettingsAware ().

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

把默认应用程序字体改为 font 。若 className 有传递,改变仅适用的类继承 className (作为报告通过 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.

警告: 不要将使用此函数结合 Qt 样式表 . 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 (),和 QWidget::setFont ().

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

将聚焦导航 Qt 应使用的种类设为 mode .

此特征只可用于 Qt for Embedded Linux。

该函数在 Qt 4.6 引入。

另请参阅 navigationMode () 和 keypadNavigationEnabled ().

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

Changes the default application palette to palette .

className is passed, the change applies only to widgets that inherit className (作为报告通过 QObject::inherits ()). If className is left 0, the change affects all widgets, thus overriding any previously set class specific palettes.

调色板可能根据当前 GUI 样式改变在 QStyle::polish ().

警告: 不要将使用此函数结合 Qt 样式表 . 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".

注意: 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 (),和 QStyle::polish ().

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

将应用程序的 GUI 样式设为 style 。样式对象的所有权被转移给 QApplication ,所以 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.

用法范例:

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.

警告: Qt 样式表目前不支持自定义 QStyle 子类。计划在某些未来发行中解决此问题。

另请参阅 style (), QStyle , setPalette (),和 desktopSettingsAware ().

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

这是重载函数。

请求 QStyle 对象为 style QStyleFactory .

字符串必须是某一 QStyleFactory::keys (), typically one of "windows", "fusion", "windowsxp", or "macintosh". Style names are case insensitive.

Returns 0 if an unknown style 被传递,否则 QStyle 返回对象被设为应用程序的 GUI 样式。

警告: 要确保应用程序样式设置正确,最好调用此函数先于 QApplication 构造函数,若可能的话。

[static] QStyle *QApplication:: style ()

另请参阅 setStyle () 和 QStyle .

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

返回的顶层 Widget 在给定 point ; returns 0 if there is no such widget.

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

这是重载函数。

返回的顶层 Widget 在 point ( x , y );返回 0 若没有这种 Widget。

[static] QWidgetList QApplication:: topLevelWidgets ()

返回应用程序顶层 Widget (窗口) 的列表。

注意: 某些顶层 Widget 可能被隐藏,例如:工具提示,若目前未展示工具提示。

范例:

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

另请参阅 allWidgets (), QWidget::isWindow (),和 QWidget::isHidden ().

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

返回 Widget,在全局屏幕位置 point , or 0 if there is no Qt widget there.

此函数可能很慢。

另请参阅 QCursor::pos (), QWidget::grabMouse (),和 QWidget::grabKeyboard ().

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

这是重载函数。

返回 Widget,在全局屏幕位置 ( x , y ), or 0 if there is no Qt widget there.

宏文档编制

qApp

引用唯一应用程序对象的全局指针。它相当于 QCoreApplication::instance (),但铸造作为 QApplication 指针,因此才有效当唯一应用程序对象为 QApplication .

另请参阅 QCoreApplication::instance () 和 qGuiApp .