QApplication露
The
QApplicationclass manages the GUI application鈥檚 control flow and main settings. More鈥

Synopsis露
Functions露
def
styleSheet()
Slots露
def
autoSipEnabled()def
setAutoSipEnabled(enabled)def
setStyleSheet(sheet)
Signals露
def
focusChanged(old, now)
Static functions露
def
aboutQt()def
activeModalWidget()def
activePopupWidget()def
activeWindow()def
alert(widget[, duration=0])def
allWidgets()def
beep()def
closeAllWindows()def
colorSpec()def
cursorFlashTime()def
desktop()def
doubleClickInterval()def
focusWidget()def
font(arg__1)def
font(className)def
fontMetrics()def
globalStrut()def
isEffectEnabled(arg__1)def
keyboardInputInterval()def
palette(arg__1)def
palette(className)def
setActiveWindow(act)def
setColorSpec(arg__1)def
setCursorFlashTime(arg__1)def
setDoubleClickInterval(arg__1)def
setEffectEnabled(arg__1[, enable=true])def
setFont(arg__1[, className=None])def
setGlobalStrut(arg__1)def
setKeyboardInputInterval(arg__1)def
setPalette(arg__1[, className=None])def
setStartDragDistance(l)def
setStartDragTime(ms)def
setStyle(arg__1)def
setStyle(arg__1)def
setWheelScrollLines(arg__1)def
startDragDistance()def
startDragTime()def
style()def
topLevelAt(x, y)def
topLevelWidgets()def
wheelScrollLines()def
widgetAt(p)def
widgetAt(x, y)
Detailed Description露
QApplicationspecializesQGuiApplicationwith some functionality needed forQWidget-based applications. It handles widget specific initialization, finalization.For any GUI application using Qt, there is precisely one
QApplicationobject, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidgetbased Qt applications, useQGuiApplicationinstead, 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
QCoreApplicationto 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
QApplicationobject is accessible through theinstance()function that returns a pointer equivalent to the globalqApppointer.
QApplication鈥榮 main areas of responsibility are:
It initializes the application with the user鈥檚 desktop settings such as
palette(),font()anddoubleClickInterval(). 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()andpostEvent()you can send your own events to widgets.It parses common command line arguments and sets its internal state accordingly. See the
constructor documentationbelow for more details.It defines the application鈥檚 look and feel, which is encapsulated in a
QStyleobject. This can be changed at runtime withsetStyle().It provides localization of strings that are visible to the user via
translate().It provides some magical objects like the
desktop()and theclipboard().It knows about the application鈥檚 windows. You can ask which widget is at a certain position using
widgetAt(), get a list oftopLevelWidgets()andcloseAllWindows(), etc.It manages the application鈥檚 mouse cursor handling, see
setOverrideCursor()Since the
QApplicationobject does so much initialization, it must be created before any other objects related to the user interface are created.QApplicationalso deals with common command line arguments. Hence, it is usually a good idea to create it before any interpretation or modification ofargvis done in the application itself.
Groups of functions
System settings
desktopSettingsAware(),setDesktopSettingsAware(),cursorFlashTime(),setCursorFlashTime(),doubleClickInterval(),setDoubleClickInterval(),setKeyboardInputInterval(),wheelScrollLines(),setWheelScrollLines(),palette(),setPalette(),font(),setFont(),fontMetrics().Event handling
exec(),processEvents(),exit(),quit().sendEvent(),postEvent(),sendPostedEvents(),removePostedEvents(),hasPendingEvents(),notify().GUI Styles
style(),setStyle().Text handling
installTranslator(),removeTranslator()translate().Widgets
allWidgets(),topLevelWidgets(),desktop(),activePopupWidget(),activeModalWidget(),clipboard(),focusWidget(),activeWindow(),widgetAt().Advanced cursor handling
overrideCursor(),setOverrideCursor(),restoreOverrideCursor().Miscellaneous
closeAllWindows(),startingUp(),closingDown().See also
QCoreApplicationQAbstractEventDispatcherQEventLoopQSettings
- class PySide2.QtWidgets.QApplication露
PySide2.QtWidgets.QApplication(arg__1)
- Parameters:
arg__1 鈥 list of strings
- PySide2.QtWidgets.QApplication.ColorSpec露
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
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
aboutQt().
- static PySide2.QtWidgets.QApplication.activeModalWidget()露
- Return type:
Returns the active modal widget.
A modal widget is a special top-level widget which is a subclass of
QDialogthat 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.
See also
- static PySide2.QtWidgets.QApplication.activePopupWidget()露
- Return type:
Returns the active popup widget.
A popup widget is a special top-level widget that sets the
Qt::WType_Popupwidget flag, e.g. theQMenuwidget. 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.
See also
- static PySide2.QtWidgets.QApplication.activeWindow()露
- Return type:
Returns the application top-level window that has the keyboard input focus, or
Noneif no application window has the focus. There might be an even if there is nofocusWidget(), for example if no widget in that window accepts key events.
- static PySide2.QtWidgets.QApplication.alert(widget[, duration=0])露
- Parameters:
widget 鈥
PySide2.QtWidgets.QWidgetduration 鈥 int
Causes an alert to be shown for
widgetif the window is not the active window. The alert is shown formsecmilliseconds. Ifmsecis 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鈥檚 taskbar entry to flash for a time. If
msecis 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 鈥渄emands 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 (
isEmpty()) if there are no widgets.Note
Some of the widgets may be hidden.
Example:
def updateAllWidgets(): for widget in QApplication.allWidgets() widget.update()
See also
- PySide2.QtWidgets.QApplication.autoSipEnabled()露
- Return type:
bool
This property holds toggles automatic SIP (software input panel) visibility.
Set this property to
trueto 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.
- 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.setShortcut(tr("Ctrl+Q")) exitAct.setStatusTip(tr("Exit the application")) exitAct.triggered.connect(qApp.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
quitOnLastWindowClosedto false.See also
quitOnLastWindowClosedlastWindowClosed()close()closeEvent()lastWindowClosed()quit()topLevelWidgets()isWindow()
- static PySide2.QtWidgets.QApplication.colorSpec()露
- Return type:
int
Note
This function is deprecated.
Returns the color specification.
See also
- static PySide2.QtWidgets.QApplication.cursorFlashTime()露
- Return type:
int
This property holds the text cursor鈥檚 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.
- static PySide2.QtWidgets.QApplication.desktop()露
- Return type:
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鈥檚 geometry.
QDesktopWidgethas various functions for obtaining useful geometries upon the desktop, such asscreenGeometry()andavailableGeometry().
- static PySide2.QtWidgets.QApplication.doubleClickInterval()露
- Return type:
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鈥檚 value is used.
- PySide2.QtWidgets.QApplication.focusChanged(old, now)露
- Parameters:
- static PySide2.QtWidgets.QApplication.focusWidget()露
- Return type:
Returns the application widget that has the keyboard input focus, or
Noneif no widget in this application has the focus.See also
- static PySide2.QtWidgets.QApplication.font(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QWidget- Return type:
This is an overloaded function.
Returns the default font for the
widget. If a default font was not registered for thewidget鈥榮 class, it returns the default font of its nearest registered superclass.See also
- static PySide2.QtWidgets.QApplication.font(className)
- Parameters:
className 鈥 str
- Return type:
This is an overloaded function.
Returns the font for widgets of the given
className.
- static PySide2.QtWidgets.QApplication.fontMetrics()露
- Return type:
Returns display (screen) font metrics for the application font.
See also
font()setFont()fontMetrics()fontMetrics()
- static PySide2.QtWidgets.QApplication.globalStrut()露
- Return type:
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:
class MyWidget (QWidget): # ... def sizeHint(self): return QSize(80, 25).expandedTo(QApplication.globalStrut())
By default, this property contains a
QSizeobject with zero width and height.
- static PySide2.QtWidgets.QApplication.isEffectEnabled(arg__1)露
- Parameters:
arg__1 鈥
UIEffect- Return type:
bool
Returns
trueifeffectis enabled; otherwise returnsfalse.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.
See also
setEffectEnabled()UIEffect
- static PySide2.QtWidgets.QApplication.keyboardInputInterval()露
- Return type:
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鈥檚 value is used.
- static PySide2.QtWidgets.QApplication.palette(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QWidget- Return type:
If a
widgetis passed, the default palette for the widget鈥檚 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.See also
- static PySide2.QtWidgets.QApplication.palette(className)
- Parameters:
className 鈥 str
- Return type:
This is an overloaded function.
Returns the palette for widgets of the given
className.See also
- static PySide2.QtWidgets.QApplication.setActiveWindow(act)露
- Parameters:
Sets the active window to the
activewidget 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
activateWindow()instead.It sets the
activeWindow()andfocusWidget()attributes and sends properWindowActivate/WindowDeactivateandFocusIn/FocusOutevents 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.See also
- PySide2.QtWidgets.QApplication.setAutoSipEnabled(enabled)露
- Parameters:
enabled 鈥 bool
This property holds toggles automatic SIP (software input panel) visibility.
Set this property to
trueto 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.
- static PySide2.QtWidgets.QApplication.setColorSpec(arg__1)露
- Parameters:
arg__1 鈥 int
Note
This function is deprecated.
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
QApplicationobject.The options are:
Be aware that the
CustomColorandManyColorchoices may lead to colormap flashing: The foreground application gets (most) of the available colors, while the background windows will look less attractive.Example:
def main(): QApplication.setColorSpec(QApplication.ManyColor) QApplication app(sys.argv) ... return app.exec_()
See also
- static PySide2.QtWidgets.QApplication.setCursorFlashTime(arg__1)露
- Parameters:
arg__1 鈥 int
This property holds the text cursor鈥檚 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.
- static PySide2.QtWidgets.QApplication.setDoubleClickInterval(arg__1)露
- Parameters:
arg__1 鈥 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鈥檚 value is used.
- static PySide2.QtWidgets.QApplication.setEffectEnabled(arg__1[, enable=true])露
- Parameters:
arg__1 鈥
UIEffectenable 鈥 bool
Enables the UI effect
effectifenableis true, otherwise the effect will not be used.Note
All effects are disabled on screens running at less than 16-bit color depth.
See also
isEffectEnabled()UIEffectsetDesktopSettingsAware()
- static PySide2.QtWidgets.QApplication.setFont(arg__1[, className=None])露
- Parameters:
arg__1 鈥
PySide2.QtGui.QFontclassName 鈥 str
Changes the default application font to
font. IfclassNameis passed, the change applies only to classes that inheritclassName(as reported byinherits()).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 鈥渇ont鈥 style sheet property. To set a bold font for all QPushButtons, set the application
styleSheet()as 鈥QPushButton{ font: bold }鈥See also
- static PySide2.QtWidgets.QApplication.setGlobalStrut(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtCore.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:
class MyWidget (QWidget): # ... def sizeHint(self): return QSize(80, 25).expandedTo(QApplication.globalStrut())
By default, this property contains a
QSizeobject with zero width and height.
- static PySide2.QtWidgets.QApplication.setKeyboardInputInterval(arg__1)露
- Parameters:
arg__1 鈥 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鈥檚 value is used.
- static PySide2.QtWidgets.QApplication.setPalette(arg__1[, className=None])露
- Parameters:
arg__1 鈥
PySide2.QtGui.QPaletteclassName 鈥 str
Changes the application palette to
palette.If
classNameis passed, the change applies only to widgets that inheritclassName(as reported byinherits()). IfclassNameis 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
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 鈥渃olor鈥, 鈥渂ackground-color鈥, 鈥渟election-color鈥, 鈥渟election-background-color鈥 and 鈥渁lternate-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 Vista and macOS styles.
See also
- static PySide2.QtWidgets.QApplication.setStartDragDistance(l)露
- Parameters:
l 鈥 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鈥檚 value as the minimum distance required.
For example, if the mouse position of the click is stored in
startPosand the current position (e.g. in the mouse move event) iscurrentPos, 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鈥檛 provide a different default) is 10 pixels.
See also
startDragTime()manhattanLength()Drag and Drop
- static PySide2.QtWidgets.QApplication.setStartDragTime(ms)露
- Parameters:
ms 鈥 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鈥檚 value as the delay.
Qt also uses this delay internally, e.g. in
QTextEditandQLineEdit, for starting a drag.The default value is 500 ms.
See also
- static PySide2.QtWidgets.QApplication.setStyle(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QStyle
Sets the application鈥檚 GUI style to
style. Ownership of the style object is transferred toQApplication, soQApplicationwill 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(QWindowsStyle())
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 usestandardPalette()for the palette.Warning
Qt style sheets are currently not supported for custom
QStylesubclasses. We plan to address this in some future release.See also
style()QStylesetPalette()desktopSettingsAware()
- static PySide2.QtWidgets.QApplication.setStyle(arg__1)
- Parameters:
arg__1 鈥 str
- Return type:
- PySide2.QtWidgets.QApplication.setStyleSheet(sheet)露
- Parameters:
sheet 鈥 str
This property holds the application style sheet.
By default, this property returns an empty string unless the user specifies the
-stylesheetoption on the command line when running the application.See also
- static PySide2.QtWidgets.QApplication.setWheelScrollLines(arg__1)露
- Parameters:
arg__1 鈥 int
This property holds the number of lines to scroll a widget, when the mouse wheel is rotated..
If the value exceeds the widget鈥檚 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鈥檚scroll mode. Scroll one line can meanscroll one itemorscroll one pixel.By default, this property has a value of 3.
See also
wheelScrollLines()
- static PySide2.QtWidgets.QApplication.startDragDistance()露
- Return type:
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鈥檚 value as the minimum distance required.
For example, if the mouse position of the click is stored in
startPosand the current position (e.g. in the mouse move event) iscurrentPos, 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鈥檛 provide a different default) is 10 pixels.
See also
startDragTime()manhattanLength()Drag and Drop
- static PySide2.QtWidgets.QApplication.startDragTime()露
- Return type:
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鈥檚 value as the delay.
Qt also uses this delay internally, e.g. in
QTextEditandQLineEdit, for starting a drag.The default value is 500 ms.
See also
- static PySide2.QtWidgets.QApplication.style()露
- Return type:
Returns the application鈥檚 style object.
See also
- PySide2.QtWidgets.QApplication.styleSheet()露
- Return type:
str
This property holds the application style sheet.
By default, this property returns an empty string unless the user specifies the
-stylesheetoption on the command line when running the application.See also
- static PySide2.QtWidgets.QApplication.topLevelAt(x, y)露
- Parameters:
x 鈥 int
y 鈥 int
- 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.
Note
Some of the top-level widgets may be hidden, for example a tooltip if no tooltip is currently shown.
Example:
def showAllHiddenTopLevelWidgets(): for widget in QApplication.topLevelWidgets(): if widget.isHidden(): widget.show()
See also
- static PySide2.QtWidgets.QApplication.wheelScrollLines()露
- Return type:
int
This property holds the number of lines to scroll a widget, when the mouse wheel is rotated..
If the value exceeds the widget鈥檚 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鈥檚scroll mode. Scroll one line can meanscroll one itemorscroll one pixel.By default, this property has a value of 3.
See also
wheelScrollLines()
- static PySide2.QtWidgets.QApplication.widgetAt(p)露
- Parameters:
- Return type:
Returns the widget at global screen position
point, orNoneif there is no Qt widget there.This function can be slow.
See also
pos()grabMouse()grabKeyboard()
- static PySide2.QtWidgets.QApplication.widgetAt(x, y)
- Parameters:
x 鈥 int
y 鈥 int
- Return type:
This is an overloaded function.
Returns the widget at global screen position (
x,y), orNoneif there is no Qt widget there.
漏 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.