QMessageBoxclass provides a modal dialog for informing the user or for asking the user a question and receiving an answer. 更多 …
def
addButton
(button)
def
addButton
(button, role)
def
addButton
(text, role)
def
button
(which)
def
buttonRole
(button)
def
buttonText
(button)
def
buttons
()
def
checkBox
()
def
clickedButton
()
def
defaultButton
()
def
detailedText
()
def
escapeButton
()
def
icon
()
def
iconPixmap
()
def
informativeText
()
def
open
(receiver, member)
def
removeButton
(button)
def
setButtonText
(button, text)
def
setCheckBox
(cb)
def
setDefaultButton
(button)
def
setDefaultButton
(button)
def
setDetailedText
(text)
def
setEscapeButton
(button)
def
setEscapeButton
(button)
def
setIcon
(arg__1)
def
setIconPixmap
(pixmap)
def
setInformativeText
(text)
def
setStandardButtons
(buttons)
def
setText
(text)
def
setTextFormat
(format)
def
setTextInteractionFlags
(flags)
def
setWindowModality
(windowModality)
def
setWindowTitle
(title)
def
standardButton
(button)
def
standardButtons
()
def
text
()
def
textFormat
()
def
textInteractionFlags
()
def
buttonClicked
(button)
def
about
(parent, title, text)
def
aboutQt
(parent[, title=””])
def
critical
(parent, title, text, button0, button1)
def
critical
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])
def
information
(parent, title, text, button0[, button1=NoButton])
def
information
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])
def
question
(parent, title, text, button0, button1)
def
question
(parent, title, text[, buttons=QMessageBox.StandardButtons(Yes | No)[, defaultButton=NoButton]])
def
standardIcon
(icon)
def
warning
(parent, title, text, button0, button1)
def
warning
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])
消息框显示首要
text以向用户发出状况警报,informative text以进一步阐述警报或向用户询问问题,和可选detailed text以提供更多数据,若用户要求。消息框也可以显示iconandstandard buttons为接受用户响应。Two APIs for using
QMessageBoxare provided, the property-based API, and the static functions. Calling one of the static functions is the simpler approach, but it is less flexible than using the property-based API, and the result is less informative. Using the property-based API is recommended.
To use the property-based API, construct an instance of
QMessageBox, set the desired properties, and callexec()to show the message. The simplest configuration is to set only themessage text特性。QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec();The user must click the OK button to dismiss the message box. The rest of the GUI is blocked until the message box is dismissed.
![]()
A better approach than just alerting the user to an event is to also ask the user what to do about it. Store the question in the
informative textproperty, and set thestandard buttonsproperty to the set of buttons you want as the set of user responses. The buttons are specified by combining values fromStandardButtonsusing the bitwise OR operator. The display order for the buttons is platform-dependent. For example, on Windows, Save is displayed to the left of Cancel, whereas on Mac OS, the order is reversed.Mark one of your standard buttons to be your
default button.QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Save); int ret = msgBox.exec();This is the approach recommended in the macOS Guidelines . Similar guidelines apply for the other platforms, but note the different ways the
informative textis handled for different platforms.![]()
exec()slot returns theStandardButtonsvalue of the button that was clicked.switch (ret) { case QMessageBox::Save: // Save was clicked break; case QMessageBox::Discard: // Don't Save was clicked break; case QMessageBox::Cancel: // Cancel was clicked break; default: // should never be reached break; }To give the user more information to help him answer the question, set the
detailed textproperty. If thedetailed textproperty is set, the Show Details… button will be shown.![]()
Clicking the Show Details… button displays the detailed text.
![]()
detailed textproperty is always interpreted as plain text. Themain textandinformative textproperties can be either plain text or rich text. These strings are interpreted according to the setting of thetext formatproperty. The default setting isauto-text.Note that for some plain text strings containing XML meta-characters, the auto-text
rich text detection testmay fail causing your plain text string to be interpreted incorrectly as rich text. In these rare cases, useconvertFromPlainText()to convert your plain text string to a visually equivalent rich text string, or set thetext formatproperty explicitly withsetTextFormat().
QMessageBoxsupports four predefined message severity levels, or message types, which really only differ in the predefined icon they each show. Specify one of the four predefined message types by setting theiconproperty to one of thepredefined icons. The following rules are guidelines:
![]()
询问For asking a question during normal operations.
![]()
InformationFor reporting information about normal operations.
![]()
警告For reporting non-critical errors.
![]()
CriticalFor reporting critical errors.
Predefined iconsare not defined byQMessageBox, but provided by the style. The default value isNo Icon. The message boxes are otherwise the same for all cases. When using a standard icon, use the one recommended in the table, or use the one recommended by the style guidelines for your platform. If none of the standard icons is right for your message box, you can use a custom icon by setting theicon pixmapproperty instead of setting theicon特性。In summary, to set an icon, use either
setIcon()for one of the standard icons, orsetIconPixmap()for a custom icon.
Building message boxes with the static functions API, although convenient, is less flexible than using the property-based API, because the static function signatures lack parameters for setting the
informative textanddetailed textproperties. One work-around for this has been to use thetitleparameter as the message box main text and thetextparameter as the message box informative text. Because this has the obvious drawback of making a less readable message box, platform guidelines do not recommend it. The Microsoft Windows User Interface Guidelines recommend using theapplication name作为window's title, which means that if you have an informative text in addition to your main text, you must concatenate it to thetext参数。Note that the static function signatures have changed with respect to their button parameters, which are now used to set the
standard buttons和default button.Static functions are available for creating
information(),question(),warning(),和critical()message boxes.int ret = QMessageBox::warning(this, tr("My Application"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);标准对话框 example shows how to use
QMessageBoxand the other built-in Qt dialogs.
若
standard buttonsare not flexible enough for your message box, you can use theaddButton()overload that takes a text and aButtonRoleto add custom buttons. TheButtonRoleis used byQMessageBoxto determine the ordering of the buttons on screen (which varies according to the platform). You can test the value ofclickedButton()after callingexec()。例如,QMessageBox msgBox; QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); msgBox.exec(); if (msgBox.clickedButton() == connectButton) { // connect } else if (msgBox.clickedButton() == abortButton) { // abort }
The default button (i.e., the button activated when Enter is pressed) can be specified using
setDefaultButton(). If a default button is not specified,QMessageBoxtries to find one based on thebutton rolesof the buttons used in the message box.The escape button (the button activated when Esc is pressed) can be specified using
setEscapeButton(). If an escape button is not specified,QMessageBoxtries to find one using these rules:
If there is only one button, it is the button activated when Esc is pressed.
If there is a
Cancelbutton, it is the button activated when Esc is pressed.If there is exactly one button having either
the Reject role或the No role, it is the button activated when Esc is pressed.When an escape button can’t be determined using these rules, pressing Esc has no effect.
另请参阅
QDialogButtonBoxGUI Design Handbook: Message Box 标准对话框范例 应用程序范例
QMessageBox
(
icon
,
title
,
text
[
,
buttons=QMessageBox.NoButton
[
,
parent=None
[
,
flags=Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint
]
]
]
)
¶
QMessageBox([parent=None])
- param parent
- param title
unicode
- param buttons
StandardButtons- param flags
WindowFlags- param icon
Icon- param text
unicode
Constructs a message box with the given
icon
,
title
,
text
, and standard
buttons
. Standard or custom buttons can be added at any time using
addButton()
。
parent
and
f
自变量被传递给
QDialog
构造函数。
消息框是
application
modal
对话框。
在 macOS,若
parent
不是
None
and you want your message box to appear as a
Sheet
of that parent, set the message box’s
window
modality
to
WindowModal
(default). Otherwise, the message box will be a standard dialog.
Constructs a message box with no text and no buttons.
parent
会被传递给
QDialog
构造函数。
On macOS, if you want your message box to appear as a
Sheet
of its
parent
, set the message box’s
window
modality
to
WindowModal
or use
open()
. Otherwise, the message box will be a standard dialog.
PySide2.QtWidgets.QMessageBox.
Icon
¶
This enum has the following values:
|
常量 |
描述 |
|---|---|
|
QMessageBox.NoIcon |
the message box does not have any icon. |
|
QMessageBox.Question |
an icon indicating that the message is asking a question. |
|
QMessageBox.Information |
an icon indicating that the message is nothing out of the ordinary. |
|
QMessageBox.Warning |
an icon indicating that the message is a warning, but can be dealt with. |
|
QMessageBox.Critical |
an icon indicating that the message represents a critical problem. |
PySide2.QtWidgets.QMessageBox.
ButtonRole
¶
This enum describes the roles that can be used to describe buttons in the button box. Combinations of these roles are as flags used to describe different aspects of their behavior.
|
常量 |
描述 |
|---|---|
|
QMessageBox.InvalidRole |
The button is invalid. |
|
QMessageBox.AcceptRole |
Clicking the button causes the dialog to be accepted (e.g. OK). |
|
QMessageBox.RejectRole |
Clicking the button causes the dialog to be rejected (e.g. Cancel). |
|
QMessageBox.DestructiveRole |
Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog. |
|
QMessageBox.ActionRole |
Clicking the button causes changes to the elements within the dialog. |
|
QMessageBox.HelpRole |
The button can be clicked to request help. |
|
QMessageBox.YesRole |
The button is a “Yes”-like button. |
|
QMessageBox.NoRole |
The button is a “No”-like button. |
|
QMessageBox.ApplyRole |
The button applies current changes. |
|
QMessageBox.ResetRole |
The button resets the dialog’s fields to default values. |
另请参阅
StandardButton
PySide2.QtWidgets.QMessageBox.
StandardButton
¶
These enums describe flags for standard buttons. Each button has a defined
ButtonRole
.
|
常量 |
描述 |
|---|---|
|
QMessageBox.Ok |
An “OK” button defined with the
|
|
QMessageBox.Open |
An “Open” button defined with the
|
|
QMessageBox.Save |
A “Save” button defined with the
|
|
QMessageBox.Cancel |
A “Cancel” button defined with the
|
|
QMessageBox.Close |
A “Close” button defined with the
|
|
QMessageBox.Discard |
A “Discard” or “Don’t Save” button, depending on the platform, defined with the
|
|
QMessageBox.Apply |
An “Apply” button defined with the
|
|
QMessageBox.Reset |
A “Reset” button defined with the
|
|
QMessageBox.RestoreDefaults |
A “Restore Defaults” button defined with the
|
|
QMessageBox.Help |
A “Help” button defined with the
|
|
QMessageBox.SaveAll |
A “Save All” button defined with the
|
|
QMessageBox.Yes |
A “Yes” button defined with the
|
|
QMessageBox.YesToAll |
A “Yes to All” button defined with the
|
|
QMessageBox.No |
A “No” button defined with the
|
|
QMessageBox.NoToAll |
A “No to All” button defined with the
|
|
QMessageBox.Abort |
An “Abort” button defined with the
|
|
QMessageBox.Retry |
A “Retry” button defined with the
|
|
QMessageBox.Ignore |
An “Ignore” button defined with the
|
|
QMessageBox.NoButton |
An invalid button. |
以下值已过时:
|
常量 |
描述 |
|---|---|
|
QMessageBox.YesAll |
Use instead. |
|
QMessageBox.NoAll |
Use instead. |
|
QMessageBox.Default |
使用
|
|
QMessageBox.Escape |
调用
|
|
QMessageBox.FlagMask |
|
|
QMessageBox.ButtonMask |
另请参阅
ButtonRole
standardButtons
PySide2.QtWidgets.QMessageBox.
about
(
parent
,
title
,
text
)
¶
parent
–
QWidget
title – unicode
text – unicode
Displays a simple about box with title
title
和文本
text
. The about box’s parent is
parent
.
looks for a suitable icon in four locations:
It prefers
parent->icon()
if that exists.
If not, it tries the top-level widget containing
parent
.
If that fails, it tries the
PySide2.QtWidgets.QApplication.activeWindow()
As a last resort it uses the Information icon.
The about box has a single button labelled “OK”. On macOS, the about box is popped up as a modeless window; on other platforms, it is currently application modal.
PySide2.QtWidgets.QMessageBox.
aboutQt
(
parent
[
,
title=""
]
)
¶
parent
–
QWidget
title – unicode
Displays a simple message box about Qt, with the given
title
and centered over
parent
(if
parent
不是
None
). 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 菜单 范例。
QApplication
provides this functionality as a slot.
On macOS, the about box is popped up as a modeless window; on other platforms, it is currently application modal.
另请参阅
PySide2.QtWidgets.QMessageBox.
addButton
(
button
,
role
)
¶
button
–
QAbstractButton
role
–
ButtonRole
添加给定
button
to the message box with the specified
role
.
PySide2.QtWidgets.QMessageBox.
addButton
(
button
)
¶
button
–
StandardButton
这是重载函数。
添加标准
button
to the message box if it is valid to do so, and returns the push button.
另请参阅
PySide2.QtWidgets.QMessageBox.
addButton
(
text
,
role
)
¶
text – unicode
role
–
ButtonRole
which
–
StandardButton
Returns a pointer corresponding to the standard button
which
,或
None
if the standard button doesn’t exist in this message box.
button
–
QAbstractButton
button
–
QAbstractButton
Returns the button role for the specified
button
. This function returns
InvalidRole
if
button
is
None
or has not been added to the message box.
另请参阅
button
–
int
unicode
Returns the text of the message box button
button
, or an empty string if the message box does not contain the button.
使用
button()
and
text()
代替。
另请参阅
Returns a list of all the buttons that have been added to the message box.
PySide2.QtWidgets.QMessageBox.
checkBox
(
)
¶
Returns the checkbox shown on the dialog. This is
None
if no checkbox is set.
另请参阅
PySide2.QtWidgets.QMessageBox.
clickedButton
(
)
¶
Returns the button that was clicked by the user, or
None
if the user hit the Esc key and no
escape
button
was set.
若
exec()
hasn’t been called yet, returns nullptr.
范例:
QMessageBox messageBox(this);
QAbstractButton *disconnectButton =
messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
...
messageBox.exec();
if (messageBox.clickedButton() == disconnectButton) {
...
}
另请参阅
PySide2.QtWidgets.QMessageBox.
critical
(
parent
,
title
,
text
[
,
buttons=QMessageBox.Ok
[
,
defaultButton=NoButton
]
]
)
¶
parent
–
QWidget
title – unicode
text – unicode
buttons
–
StandardButtons
defaultButton
–
StandardButton
PySide2.QtWidgets.QMessageBox.
critical
(
parent
,
title
,
text
,
button0
,
button1
)
¶
parent
–
QWidget
title – unicode
text – unicode
button0
–
StandardButton
button1
–
StandardButton
int
PySide2.QtWidgets.QMessageBox.
defaultButton
(
)
¶
Returns the button that should be the message box’s
default
button
。返回 nullptr 若未设置默认按钮。
PySide2.QtWidgets.QMessageBox.
detailedText
(
)
¶
unicode
另请参阅
PySide2.QtWidgets.QMessageBox.
escapeButton
(
)
¶
返回被激活按钮,当按下 Esc 键时。
默认情况下,
QMessageBox
attempts to automatically detect an escape button as follows:
If there is only one button, it is made the escape button.
If there is a
Cancel
button, it is made the escape button.
On macOS only, if there is exactly one button with the role
RejectRole
, it is made the escape button.
When an escape button could not be automatically detected, pressing Esc has no effect.
PySide2.QtWidgets.QMessageBox.
iconPixmap
(
)
¶
QPixmap
另请参阅
PySide2.QtWidgets.QMessageBox.
information
(
parent
,
title
,
text
[
,
buttons=QMessageBox.Ok
[
,
defaultButton=NoButton
]
]
)
¶
parent
–
QWidget
title – unicode
text – unicode
buttons
–
StandardButtons
defaultButton
–
StandardButton
PySide2.QtWidgets.QMessageBox.
information
(
parent
,
title
,
text
,
button0
[
,
button1=NoButton
]
)
¶
parent
–
QWidget
title – unicode
text – unicode
button0
–
StandardButton
button1
–
StandardButton
PySide2.QtWidgets.QMessageBox.
informativeText
(
)
¶
unicode
另请参阅
PySide2.QtWidgets.QMessageBox.
open
(
receiver
,
member
)
¶
receiver
–
QObject
member – str
打开对话框并连接其
finished()
or
buttonClicked()
signal to the slot specified by
receiver
and
member
。若槽在
member
has a pointer for its first parameter the connection is to
buttonClicked()
, otherwise the connection is to
finished()
.
信号将断开槽连接,当对话框被关闭时。
PySide2.QtWidgets.QMessageBox.
question
(
parent
,
title
,
text
,
button0
,
button1
)
¶
parent
–
QWidget
title – unicode
text – unicode
button0
–
StandardButton
button1
–
StandardButton
int
PySide2.QtWidgets.QMessageBox.
question
(
parent
,
title
,
text
[
,
buttons=QMessageBox.StandardButtons(Yes | No)
[
,
defaultButton=NoButton
]
]
)
¶
parent
–
QWidget
title – unicode
text – unicode
buttons
–
StandardButtons
defaultButton
–
StandardButton
PySide2.QtWidgets.QMessageBox.
removeButton
(
button
)
¶
button
–
QAbstractButton
移除
button
从按钮框但不删除它。
PySide2.QtWidgets.QMessageBox.
setButtonText
(
button
,
text
)
¶
button
–
int
text – unicode
Sets the text of the message box button
button
to
text
. Setting the text of a button that is not in the message box is silently ignored.
使用
addButton()
代替。
另请参阅
PySide2.QtWidgets.QMessageBox.
setCheckBox
(
cb
)
¶
cb
–
QCheckBox
Sets the checkbox
cb
on the message dialog. The message box takes ownership of the checkbox. The argument
cb
可以是
None
to remove an existing checkbox from the message box.
另请参阅
PySide2.QtWidgets.QMessageBox.
setDefaultButton
(
button
)
¶
button
–
StandardButton
Sets the message box’s
default
button
to
button
.
另请参阅
PySide2.QtWidgets.QMessageBox.
setDefaultButton
(
button
)
¶
button
–
QPushButton
Sets the message box’s
default
button
to
button
.
PySide2.QtWidgets.QMessageBox.
setDetailedText
(
text
)
¶
text – unicode
另请参阅
PySide2.QtWidgets.QMessageBox.
setEscapeButton
(
button
)
¶
button
–
QAbstractButton
Sets the button that gets activated when the Escape key is pressed to
button
.
PySide2.QtWidgets.QMessageBox.
setEscapeButton
(
button
)
¶
button
–
StandardButton
Sets the buttons that gets activated when the Escape key is pressed to
button
.
PySide2.QtWidgets.QMessageBox.
setIconPixmap
(
pixmap
)
¶
pixmap
–
QPixmap
另请参阅
PySide2.QtWidgets.QMessageBox.
setInformativeText
(
text
)
¶
text – unicode
另请参阅
PySide2.QtWidgets.QMessageBox.
setStandardButtons
(
buttons
)
¶
buttons
–
StandardButtons
另请参阅
PySide2.QtWidgets.QMessageBox.
setTextFormat
(
format
)
¶
format
–
TextFormat
另请参阅
PySide2.QtWidgets.QMessageBox.
setTextInteractionFlags
(
flags
)
¶
flags
–
TextInteractionFlags
PySide2.QtWidgets.QMessageBox.
setWindowModality
(
windowModality
)
¶
windowModality
–
WindowModality
此函数投影
setWindowModality()
.
Sets the modality of the message box to
windowModality
.
On macOS, if the modality is set to
WindowModal
and the message box has a parent, then the message box will be a
Sheet
,否则消息框将是标准对话框。
PySide2.QtWidgets.QMessageBox.
setWindowTitle
(
title
)
¶
title – unicode
此函数投影
setWindowTitle()
.
Sets the title of the message box to
title
. On macOS, the window title is ignored (as required by the macOS Guidelines).
PySide2.QtWidgets.QMessageBox.
standardButton
(
button
)
¶
button
–
QAbstractButton
Returns the standard button enum value corresponding to the given
button
,或
NoButton
若给定
button
isn’t a standard button.
PySide2.QtWidgets.QMessageBox.
standardButtons
(
)
¶
StandardButtons
另请参阅
PySide2.QtWidgets.QMessageBox.
standardIcon
(
icon
)
¶
icon
–
Icon
QPixmap
Returns the pixmap used for a standard icon. This allows the pixmaps to be used in more complex message boxes.
icon
specifies the required icon, e.g.
询问
,
Information
,
警告
or
Critical
.
调用
standardIcon()
with
SP_MessageBoxInformation
etc. instead.
PySide2.QtWidgets.QMessageBox.
textFormat
(
)
¶
TextFormat
另请参阅
PySide2.QtWidgets.QMessageBox.
textInteractionFlags
(
)
¶
TextInteractionFlags
PySide2.QtWidgets.QMessageBox.
warning
(
parent
,
title
,
text
[
,
buttons=QMessageBox.Ok
[
,
defaultButton=NoButton
]
]
)
¶
parent
–
QWidget
title – unicode
text – unicode
buttons
–
StandardButtons
defaultButton
–
StandardButton
PySide2.QtWidgets.QMessageBox.
warning
(
parent
,
title
,
text
,
button0
,
button1
)
¶
parent
–
QWidget
title – unicode
text – unicode
button0
–
StandardButton
button1
–
StandardButton
int