Static functions

Detailed Description

The PySide2.QtWidgets.QApplication class manages the GUI application’s control flow and main settings.

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

For any GUI application using Qt, there is precisely one PySide2.QtWidgets.QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non- PySide2.QtWidgets.QWidget based Qt applications, use PySide2.QtGui.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 PySide2.QtCore.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:

def main():
    useGUI = not '-no-gui' in sys.argv
    app = QApplication(sys.argv) if useGUI else QCoreApplication(sys.argv)
    return app.exec_()

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

PySide2.QtWidgets.QApplication ‘s main areas of responsibility are:

Since the PySide2.QtWidgets.QApplication object does so much initialization, it must be created before any other objects related to the user interface are created. PySide2.QtWidgets.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 settings PySide2.QtGui.QGuiApplication.desktopSettingsAware() , PySide2.QtGui.QGuiApplication.setDesktopSettingsAware() , PySide2.QtWidgets.QApplication.cursorFlashTime() , PySide2.QtWidgets.QApplication.setCursorFlashTime() , PySide2.QtWidgets.QApplication.doubleClickInterval() , PySide2.QtWidgets.QApplication.setDoubleClickInterval() , PySide2.QtWidgets.QApplication.setKeyboardInputInterval() , PySide2.QtWidgets.QApplication.wheelScrollLines() , PySide2.QtWidgets.QApplication.setWheelScrollLines() , PySide2.QtWidgets.QApplication.palette() , PySide2.QtWidgets.QApplication.setPalette() , PySide2.QtWidgets.QApplication.font() , PySide2.QtWidgets.QApplication.setFont() , PySide2.QtWidgets.QApplication.fontMetrics() .
Event handling exec() , PySide2.QtCore.QCoreApplication.processEvents() , PySide2.QtCore.QCoreApplication.exit() , PySide2.QtCore.QCoreApplication.quit() . PySide2.QtCore.QCoreApplication.sendEvent() , PySide2.QtCore.QCoreApplication.postEvent() , PySide2.QtCore.QCoreApplication.sendPostedEvents() , PySide2.QtCore.QCoreApplication.removePostedEvents() , PySide2.QtCore.QCoreApplication.hasPendingEvents() , PySide2.QtWidgets.QApplication.notify() .
GUI Styles , PySide2.QtWidgets.QApplication.setStyle() .
Color usage PySide2.QtWidgets.QApplication.colorSpec() , PySide2.QtWidgets.QApplication.setColorSpec() .
Text handling PySide2.QtCore.QCoreApplication.installTranslator() , PySide2.QtCore.QCoreApplication.removeTranslator() PySide2.QtCore.QCoreApplication.translate() .
Widgets PySide2.QtWidgets.QApplication.allWidgets() , PySide2.QtWidgets.QApplication.topLevelWidgets() , PySide2.QtWidgets.QApplication.desktop() , PySide2.QtWidgets.QApplication.activePopupWidget() , PySide2.QtWidgets.QApplication.activeModalWidget() , PySide2.QtGui.QGuiApplication.clipboard() , PySide2.QtWidgets.QApplication.focusWidget() , PySide2.QtWidgets.QApplication.activeWindow() , PySide2.QtWidgets.QApplication.widgetAt() .
Advanced cursor handling PySide2.QtGui.QGuiApplication.overrideCursor() , PySide2.QtGui.QGuiApplication.setOverrideCursor() , PySide2.QtGui.QGuiApplication.restoreOverrideCursor() .
Miscellaneous PySide2.QtWidgets.QApplication.closeAllWindows() , PySide2.QtCore.QCoreApplication.startingUp() , PySide2.QtCore.QCoreApplication.closingDown() .
class PySide2.QtWidgets.QApplication
class PySide2.QtWidgets.QApplication(arg__1)
Parameters:arg__1 – list of strings
Constant Description
QApplication.NormalColor the default color allocation policy
QApplication.CustomColor the same as for X11; allocates colors to a palette on demand under Windows
QApplication.ManyColor the right choice for applications that use thousands of colors

See PySide2.QtWidgets.QApplication.setColorSpec() for full details.

static PySide2.QtWidgets.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 PySide2.QtWidgets.QApplication.activeModalWidget()
Return type:PySide2.QtWidgets.QWidget

Returns the active modal widget.

A modal widget is a special top-level widget which is a subclass of PySide2.QtWidgets.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.

static PySide2.QtWidgets.QApplication.activePopupWidget()
Return type:PySide2.QtWidgets.QWidget

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 PySide2.QtWidgets.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.

static PySide2.QtWidgets.QApplication.activeWindow()
Return type:PySide2.QtWidgets.QWidget

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 even if there is no PySide2.QtWidgets.QApplication.focusWidget() , for example if no widget in that window accepts key events.

static PySide2.QtWidgets.QApplication.alert(widget[, duration=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.

static PySide2.QtWidgets.QApplication.allWidgets()
Return type:

Returns a list of all the widgets in the application.

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


Some of the widgets may be hidden.


def updateAllWidgets():
    for widget in QApplication.allWidgets()
Return type:PySide2.QtCore.bool
static PySide2.QtWidgets.QApplication.beep()

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

static PySide2.QtWidgets.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:

exitAct = QAction(tr("E&xit"), self)
exitAct.setStatusTip(tr("Exit the application")")
connect(exitAct, SIGNAL("triggered()"), qApp, SLOT("closeAllWindows()"))

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 PySide2.QtGui.QGuiApplication.quitOnLastWindowClosed() to false.

See also

PySide2.QtGui.QGuiApplication.quitOnLastWindowClosed() PySide2.QtGui.QGuiApplication.lastWindowClosed() QWidget.close() QWidget.closeEvent() PySide2.QtGui.QGuiApplication.lastWindowClosed() QCoreApplication.quit() PySide2.QtWidgets.QApplication.topLevelWidgets() QWidget.isWindow()

static PySide2.QtWidgets.QApplication.colorSpec()

Returns the color specification.

static PySide2.QtWidgets.QApplication.cursorFlashTime()
static PySide2.QtWidgets.QApplication.desktop()
Return type:PySide2.QtWidgets.QDesktopWidget

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. PySide2.QtWidgets.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.

static PySide2.QtWidgets.QApplication.doubleClickInterval()
PySide2.QtWidgets.QApplication.focusChanged(old, now)
static PySide2.QtWidgets.QApplication.focusWidget()
Return type:PySide2.QtWidgets.QWidget

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

static PySide2.QtWidgets.QApplication.font(className)
Parameters:className – str
Return type:PySide2.QtGui.QFont

This is an overloaded function.

Returns the font for widgets of the given className .

static PySide2.QtWidgets.QApplication.font(arg__1)
Return type:PySide2.QtGui.QFont

This is an overloaded function.

Returns the default font for the widget .

static PySide2.QtWidgets.QApplication.fontMetrics()
Return type:PySide2.QtGui.QFontMetrics

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

static PySide2.QtWidgets.QApplication.globalStrut()
Return type:PySide2.QtCore.QSize
static PySide2.QtWidgets.QApplication.isEffectEnabled(arg__1)
Return type:PySide2.QtCore.bool

Returns true if effect is enabled; otherwise returns false .

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


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

static PySide2.QtWidgets.QApplication.keyboardInputInterval()
static PySide2.QtWidgets.QApplication.palette(className)
Parameters:className – str
Return type:PySide2.QtGui.QPalette

This is an overloaded function.

Returns the palette for widgets of the given className .

static PySide2.QtWidgets.QApplication.palette(arg__1)
Return type:PySide2.QtGui.QPalette

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.

static PySide2.QtWidgets.QApplication.setActiveWindow(act)

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


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

It sets the PySide2.QtWidgets.QApplication.activeWindow() and PySide2.QtWidgets.QApplication.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.

static PySide2.QtWidgets.QApplication.setColorSpec(arg__1)

Sets the color specification for the application to spec .

This call has no effect.

The color specification controls how the application allocates colors when run on a display with a limited amount of colors, e.g. 8 bit / 256 color displays.

The color specification must be set before you create the PySide2.QtWidgets.QApplication object.

The options are:

Be aware that the CustomColor and ManyColor choices may lead to colormap flashing: The foreground application gets (most) of the available colors, while the background windows will look less attractive.


def main():
    QApplication app(sys.argv)
    return app.exec_()
static PySide2.QtWidgets.QApplication.setCursorFlashTime(arg__1)
static PySide2.QtWidgets.QApplication.setDoubleClickInterval(arg__1)
static PySide2.QtWidgets.QApplication.setEffectEnabled(arg__1[, enable=true])
  • arg__1PySide2.QtCore.Qt.UIEffect
  • enablePySide2.QtCore.bool

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


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

See also

PySide2.QtWidgets.QApplication.isEffectEnabled() Qt.UIEffect PySide2.QtGui.QGuiApplication.setDesktopSettingsAware()

static PySide2.QtWidgets.QApplication.setFont(arg__1[, className=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.


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 PySide2.QtWidgets.QApplication.styleSheet() as ” PySide2.QtWidgets.QPushButton { font: bold }”

static PySide2.QtWidgets.QApplication.setGlobalStrut(arg__1)
static PySide2.QtWidgets.QApplication.setKeyboardInputInterval(arg__1)
static PySide2.QtWidgets.QApplication.setPalette(arg__1[, className=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() .


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”.


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 Vista and macOS styles.

static PySide2.QtWidgets.QApplication.setStartDragDistance(l)
static PySide2.QtWidgets.QApplication.setStartDragTime(ms)
static PySide2.QtWidgets.QApplication.setStyle(arg__1)
Parameters:arg__1 – unicode
Return type:PySide2.QtWidgets.QStyle

This is an overloaded function.

Requests a PySide2.QtWidgets.QStyle object for style from the PySide2.QtWidgets.QStyleFactory .

The string must be one of the QStyleFactory.keys() , typically one of “windows”, “windowsvista”, “fusion”, or “macintosh”. Style names are case insensitive.

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


To ensure that the application’s style is set correctly, it is best to call this function before the PySide2.QtWidgets.QApplication constructor, if possible.

static PySide2.QtWidgets.QApplication.setStyle(arg__1)

Sets the application’s GUI style to style . Ownership of the style object is transferred to PySide2.QtWidgets.QApplication , so PySide2.QtWidgets.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:


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 PySide2.QtWidgets.QApplication , will cause the application to use QStyle.standardPalette() for the palette.


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

Parameters:sheet – unicode
static PySide2.QtWidgets.QApplication.setWheelScrollLines(arg__1)
static PySide2.QtWidgets.QApplication.startDragDistance()
static PySide2.QtWidgets.QApplication.startDragTime()
Return type:PySide2.QtWidgets.QStyle

Returns the application’s style object.

Return type:unicode
static PySide2.QtWidgets.QApplication.topLevelAt(x, y)
Return type:


This is an overloaded function.

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

static PySide2.QtWidgets.QApplication.topLevelWidgets()
Return type:

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


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


def showAllHiddenTopLevelWidgets():
    for widget in QApplication.topLevelWidgets():
        if widget.isHidden():
static PySide2.QtWidgets.QApplication.wheelScrollLines()
static PySide2.QtWidgets.QApplication.widgetAt(x, y)
Return type:


This is an overloaded function.

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

static PySide2.QtWidgets.QApplication.widgetAt(p)
Return type:PySide2.QtWidgets.QWidget

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

This function can be slow.