QApplicationclass manages the GUI application’s control flow and main settings. 更多 …
def
styleSheet
()
def
autoSipEnabled
()
def
setAutoSipEnabled
(enabled)
def
setStyleSheet
(sheet)
def
focusChanged
(old, now)
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)
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 库。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_()
QApplicationobject is accessible through theinstance()function that returns a pointer equivalent to the globalqApp指针。
QApplication‘s main areas of responsibility are:
It initializes the application with the user’s 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
构造函数 文档编制below for more details.It defines the application’s 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()和clipboard().It knows about the application’s windows. You can ask which widget is at a certain position using
widgetAt(), get a list oftopLevelWidgets()andcloseAllWindows(),等。It manages the application’s mouse cursor handling, see
setOverrideCursor()由于
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.
函数组
系统设置
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().文本处理
installTranslator(),removeTranslator()translate().Widgets
allWidgets(),topLevelWidgets(),desktop(),activePopupWidget(),activeModalWidget(),clipboard(),focusWidget(),activeWindow(),widgetAt().高级光标处理
overrideCursor(),setOverrideCursor(),restoreOverrideCursor().杂项
closeAllWindows(),startingUp(),closingDown().
QApplication
¶
QApplication(arg__1)
arg__1 – 字符串列表
PySide2.QtWidgets.QApplication.
ColorSpec
¶
|
常量 |
描述 |
|---|---|
|
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 |
见
setColorSpec()
for full details.
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 菜单 范例。
This function is a convenience slot for
aboutQt()
.
PySide2.QtWidgets.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.
PySide2.QtWidgets.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.
PySide2.QtWidgets.QApplication.
activeWindow
(
)
¶
返回拥有键盘输入聚焦的应用程序顶层窗口,或
None
if no application window has the focus. There might be an even if there is no
focusWidget()
, for example if no widget in that window accepts key events.
PySide2.QtWidgets.QApplication.
alert
(
widget
[
,
duration=0
]
)
¶
widget
–
QWidget
duration
–
int
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.
PySide2.QtWidgets.QApplication.
allWidgets
(
)
¶
返回应用程序的所有 Widget 的列表。
列表是空的 (
isEmpty()
) if there are no widgets.
注意
某些 Widget 可能被隐藏。
范例:
def updateAllWidgets():
for widget in QApplication.allWidgets()
widget.update()
PySide2.QtWidgets.QApplication.
autoSipEnabled
(
)
¶
bool
另请参阅
PySide2.QtWidgets.QApplication.
beep
(
)
¶
响铃声,使用默认音量和声音。函数 not 可用于 Qt for Embedded Linux。
PySide2.QtWidgets.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:
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
quitOnLastWindowClosed
为 false。
另请参阅
quitOnLastWindowClosed
lastWindowClosed()
close()
closeEvent()
lastWindowClosed()
quit()
topLevelWidgets()
isWindow()
PySide2.QtWidgets.QApplication.
colorSpec
(
)
¶
int
注意
此函数被弃用。
Returns the color specification.
另请参阅
PySide2.QtWidgets.QApplication.
cursorFlashTime
(
)
¶
int
另请参阅
PySide2.QtWidgets.QApplication.
desktop
(
)
¶
返回桌面 Widget (也称根窗口)。
桌面可能由多个屏幕组成,因此它将是不正确的,例如:试图
center
some widget in the desktop’s geometry.
QDesktopWidget
has various functions for obtaining useful geometries upon the desktop, such as
screenGeometry()
and
availableGeometry()
.
PySide2.QtWidgets.QApplication.
doubleClickInterval
(
)
¶
int
PySide2.QtWidgets.QApplication.
focusWidget
(
)
¶
返回拥有键盘输入聚焦的应用程序 Widget,或
None
若此应用程序的 Widget 不拥有聚焦。
PySide2.QtWidgets.QApplication.
font
(
arg__1
)
¶
arg__1
–
QWidget
QFont
这是重载函数。
返回默认字体,为
widget
.
另请参阅
PySide2.QtWidgets.QApplication.
font
(
className
)
¶
className – str
QFont
这是重载函数。
返回 Widget 字体为给定
className
.
PySide2.QtWidgets.QApplication.
fontMetrics
(
)
¶
QFontMetrics
返回用于应用程序字体的显示 (屏幕) 字体规格。
另请参阅
font()
setFont()
fontMetrics()
fontMetrics()
PySide2.QtWidgets.QApplication.
globalStrut
(
)
¶
QSize
另请参阅
PySide2.QtWidgets.QApplication.
isEffectEnabled
(
arg__1
)
¶
arg__1
–
UIEffect
bool
返回
true
if
effect
被启用;否则返回
false
.
By default, Qt will try to use the desktop settings. To prevent this, call
setDesktopSettingsAware
(false).
注意
All effects are disabled on screens running at less than 16-bit color depth.
另请参阅
setEffectEnabled()
UIEffect
PySide2.QtWidgets.QApplication.
keyboardInputInterval
(
)
¶
int
PySide2.QtWidgets.QApplication.
palette
(
arg__1
)
¶
arg__1
–
QWidget
QPalette
若
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.
另请参阅
PySide2.QtWidgets.QApplication.
palette
(
className
)
¶
className – str
QPalette
这是重载函数。
Returns the palette for widgets of the given
className
.
另请参阅
PySide2.QtWidgets.QApplication.
setActiveWindow
(
act
)
¶
act
–
QWidget
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
activateWindow()
代替。
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.
PySide2.QtWidgets.QApplication.
setAutoSipEnabled
(
enabled
)
¶
enabled
–
bool
另请参阅
PySide2.QtWidgets.QApplication.
setColorSpec
(
arg__1
)
¶
arg__1
–
int
注意
此函数被弃用。
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
QApplication
对象。
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.setColorSpec(QApplication.ManyColor)
QApplication app(sys.argv)
...
return app.exec_()
另请参阅
PySide2.QtWidgets.QApplication.
setCursorFlashTime
(
arg__1
)
¶
arg__1
–
int
另请参阅
PySide2.QtWidgets.QApplication.
setDoubleClickInterval
(
arg__1
)
¶
arg__1
–
int
PySide2.QtWidgets.QApplication.
setEffectEnabled
(
arg__1
[
,
enable=true
]
)
¶
arg__1
–
UIEffect
enable
–
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.
另请参阅
isEffectEnabled()
UIEffect
setDesktopSettingsAware()
PySide2.QtWidgets.QApplication.
setFont
(
arg__1
[
,
className=None
]
)
¶
arg__1
–
QFont
className – str
把默认应用程序字体改为
font
。若
className
is passed, the change applies only to classes that inherit
className
(as reported by
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 样式表
. 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 }”
另请参阅
PySide2.QtWidgets.QApplication.
setGlobalStrut
(
arg__1
)
¶
arg__1
–
QSize
另请参阅
PySide2.QtWidgets.QApplication.
setKeyboardInputInterval
(
arg__1
)
¶
arg__1
–
int
PySide2.QtWidgets.QApplication.
setPalette
(
arg__1
[
,
className=None
]
)
¶
arg__1
–
QPalette
className – str
将应用程序调色板更改为
palette
.
若
className
is passed, the change applies only to widgets that inherit
className
(as reported by
inherits()
)。若
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
polish()
.
警告
Do not use this function in conjunction with 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 Vista and macOS styles.
PySide2.QtWidgets.QApplication.
setStartDragDistance
(
l
)
¶
l
–
int
另请参阅
PySide2.QtWidgets.QApplication.
setStartDragTime
(
ms
)
¶
ms
–
int
另请参阅
PySide2.QtWidgets.QApplication.
setStyle
(
arg__1
)
¶
arg__1
–
QStyle
Sets the application’s GUI style to
style
. Ownership of the style object is transferred to
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(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 use
standardPalette()
for the palette.
警告
Qt style sheets are currently not supported for custom
QStyle
subclasses. We plan to address this in some future release.
另请参阅
style()
QStyle
setPalette()
desktopSettingsAware()
PySide2.QtWidgets.QApplication.
setStyleSheet
(
sheet
)
¶
sheet – unicode
另请参阅
PySide2.QtWidgets.QApplication.
setWheelScrollLines
(
arg__1
)
¶
arg__1
–
int
另请参阅
PySide2.QtWidgets.QApplication.
startDragDistance
(
)
¶
int
PySide2.QtWidgets.QApplication.
startDragTime
(
)
¶
int
另请参阅
PySide2.QtWidgets.QApplication.
style
(
)
¶
Returns the application’s style object.
另请参阅
PySide2.QtWidgets.QApplication.
styleSheet
(
)
¶
unicode
另请参阅
PySide2.QtWidgets.QApplication.
topLevelAt
(
x
,
y
)
¶
x
–
int
y
–
int
这是重载函数。
返回的顶层 Widget 在 point (
x
,
y
);返回 0 若没有这种 Widget。
PySide2.QtWidgets.QApplication.
topLevelWidgets
(
)
¶
返回应用程序顶层 Widget (窗口) 的列表。
注意
某些顶层 Widget 可能被隐藏,例如:工具提示,若目前未展示工具提示。
范例:
def showAllHiddenTopLevelWidgets():
for widget in QApplication.topLevelWidgets():
if widget.isHidden():
widget.show()
PySide2.QtWidgets.QApplication.
wheelScrollLines
(
)
¶
int
PySide2.QtWidgets.QApplication.
widgetAt
(
p
)
¶
p
–
QPoint
返回 Widget,在全局屏幕位置
point
,或
None
若那里没有 Qt Widget。
此函数可能很慢。
另请参阅
pos()
grabMouse()
grabKeyboard()