QMessageBox¶
The
QMessageBox
class provides a modal dialog for informing the user or for asking the user a question and receiving an answer. More…
Synopsis¶
Functions¶
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
standardButton
(button)def
standardButtons
()def
text
()def
textFormat
()def
textInteractionFlags
()
Signals¶
def
buttonClicked
(button)
Static functions¶
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]])
Detailed Description¶
A message box displays a primary
text
to alert the user to a situation, aninformative text
to further explain the alert or to ask the user a question, and an optionaldetailed text
to provide even more data if the user requests it. A message box can also display anicon
andstandard buttons
for accepting a user response.Two APIs for using
QMessageBox
are 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.
The Property-based API¶
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
property.msgBox = QMessageBox() 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 text
property, and set thestandard buttons
property to the set of buttons you want as the set of user responses. The buttons are specified by combining values fromStandardButtons
using 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
.msgBox = QMessageBox() 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) 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 text
is handled for different platforms.The
exec()
slot returns theStandardButtons
value of the button that was clicked.if ret == QMessageBox.Save: # Save was clicked elif ret == QMessageBox.Discard: # Don't save was clicked elif ret == QMessageBox.Cancel: # cancel was clicked else: # should never be reachedTo give the user more information to help him answer the question, set the
detailed text
property. If thedetailed text
property is set, the Show Details… button will be shown.Clicking the Show Details… button displays the detailed text.
Rich Text and the Text Format Property¶
The
detailed text
property is always interpreted as plain text. Themain text
andinformative text
properties can be either plain text or rich text. These strings are interpreted according to the setting of thetext format
property. The default setting isauto-text
.Note that for some plain text strings containing XML meta-characters, the auto-text
rich text detection test
may 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 format
property explicitly withsetTextFormat()
.
Severity Levels and the Icon and Pixmap Properties¶
QMessageBox
supports 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 theicon
property to one of thepredefined icons
. The following rules are guidelines:
Question
For asking a question during normal operations.
Information
For reporting information about normal operations.
Warning
For reporting non-critical errors.
Critical
For reporting critical errors.
Predefined icons
are 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 pixmap
property instead of setting theicon
property.In summary, to set an icon, use either
setIcon()
for one of the standard icons, orsetIconPixmap()
for a custom icon.
The Static Functions API¶
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 text
anddetailed text
properties. One work-around for this has been to use thetitle
parameter as the message box main text and thetext
parameter 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
as thewindow's title
, which means that if you have an informative text in addition to your main text, you must concatenate it to thetext
parameter.Note that the static function signatures have changed with respect to their button parameters, which are now used to set the
standard buttons
and thedefault button
.Static functions are available for creating
information()
,question()
,warning()
, andcritical()
message boxes.ret = QMessageBox.warning(self, self.tr("My Application"), self.tr("The document has been modified.\n" + \ "Do you want to save your changes?"), QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel, QMessageBox.Save)The Standard Dialogs example shows how to use
QMessageBox
and the other built-in Qt dialogs.
Advanced Usage¶
If the
standard buttons
are not flexible enough for your message box, you can use theaddButton()
overload that takes a text and aButtonRole
to add custom buttons. TheButtonRole
is used byQMessageBox
to determine the ordering of the buttons on screen (which varies according to the platform). You can test the value ofclickedButton()
after callingexec()
. For example,msgBox = QMessageBox() connectButton = msgBox.addButton(self.tr("Connect"), QMessageBox.ActionRole) abortButton = msgBox.addButton(QMessageBox.Abort) msgBox.exec_() if msgBox.clickedButton() == connectButton: # connect elif msgBox.clickedButton() == abortButton: # abort }
Default and Escape Keys¶
The default button (i.e., the button activated when Enter is pressed) can be specified using
setDefaultButton()
. If a default button is not specified,QMessageBox
tries to find one based on thebutton roles
of 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,QMessageBox
tries 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
Cancel
button, it is the button activated when Esc is pressed.If there is exactly one button having either
the Reject role
or thethe 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.
See also
QDialogButtonBox
GUI Design Handbook: Message Box Standard Dialogs Example Application Example
-
class
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 standardbuttons
. Standard or custom buttons can be added at any time usingaddButton()
. Theparent
andf
arguments are passed to theQDialog
constructor.The message box is an
application modal
dialog box.On macOS , if
parent
is not 0 and you want your message box to appear as aSheet
of that parent, set the message box’swindow modality
toWindowModal
(default). Otherwise, the message box will be a standard dialog.See also
setWindowTitle()
setText()
setIcon()
setStandardButtons()
Constructs a message box with no text and no buttons.
parent
is passed to theQDialog
constructor.On macOS , if you want your message box to appear as a
Sheet
of itsparent
, set the message box’swindow modality
toWindowModal
or useopen()
. Otherwise, the message box will be a standard dialog.
-
PySide2.QtWidgets.QMessageBox.
Icon
¶ This enum has the following values:
Constant
Description
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
¶
-
PySide2.QtWidgets.QMessageBox.
StandardButton
¶ These enums describe flags for standard buttons. Each button has a defined
ButtonRole
.Constant
Description
QMessageBox.Ok
An “OK” button defined with the
AcceptRole
.QMessageBox.Open
An “Open” button defined with the
AcceptRole
.QMessageBox.Save
A “Save” button defined with the
AcceptRole
.QMessageBox.Cancel
A “Cancel” button defined with the
RejectRole
.QMessageBox.Close
A “Close” button defined with the
RejectRole
.QMessageBox.Discard
A “Discard” or “Don’t Save” button, depending on the platform, defined with the
DestructiveRole
.QMessageBox.Apply
An “Apply” button defined with the
ApplyRole
.QMessageBox.Reset
A “Reset” button defined with the
ResetRole
.QMessageBox.RestoreDefaults
A “Restore Defaults” button defined with the
ResetRole
.QMessageBox.Help
A “Help” button defined with the
HelpRole
.QMessageBox.SaveAll
A “Save All” button defined with the
AcceptRole
.QMessageBox.Yes
A “Yes” button defined with the
YesRole
.QMessageBox.YesToAll
A “Yes to All” button defined with the
YesRole
.QMessageBox.No
A “No” button defined with the
NoRole
.QMessageBox.NoToAll
A “No to All” button defined with the
NoRole
.QMessageBox.Abort
An “Abort” button defined with the
RejectRole
.QMessageBox.Retry
A “Retry” button defined with the
AcceptRole
.QMessageBox.Ignore
An “Ignore” button defined with the
AcceptRole
.QMessageBox.NoButton
An invalid button.
The following values are obsolete:
Constant
Description
QMessageBox.YesAll
Use instead.
QMessageBox.NoAll
Use instead.
QMessageBox.Default
Use the
defaultButton
argument ofinformation()
,warning()
, etc. instead, or callsetDefaultButton()
.QMessageBox.Escape
Call
setEscapeButton()
instead.QMessageBox.FlagMask
QMessageBox.ButtonMask
See also
ButtonRole
standardButtons
-
static
PySide2.QtWidgets.QMessageBox.
about
(parent, title, text)¶ - Parameters
parent –
QWidget
title – unicode
text – unicode
Displays a simple about box with title
title
and texttext
. The about box’s parent isparent
.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.
See also
-
static
PySide2.QtWidgets.QMessageBox.
aboutQt
(parent[, title=""])¶ - Parameters
parent –
QWidget
title – unicode
Displays a simple message box about Qt, with the given
title
and centered overparent
(ifparent
is not 0). 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.
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.
See also
-
PySide2.QtWidgets.QMessageBox.
addButton
(button, role)¶ - Parameters
button –
QAbstractButton
role –
ButtonRole
Adds the given
button
to the message box with the specifiedrole
.See also
-
PySide2.QtWidgets.QMessageBox.
addButton
(button) - Parameters
button –
StandardButton
- Return type
This is an overloaded function.
Adds a standard
button
to the message box if it is valid to do so, and returns the push button.See also
-
PySide2.QtWidgets.QMessageBox.
addButton
(text, role) - Parameters
text – unicode
role –
ButtonRole
- Return type
- Parameters
which –
StandardButton
- Return type
Returns a pointer corresponding to the standard button
which
, or 0 if the standard button doesn’t exist in this message box.See also
- Parameters
button –
QAbstractButton
- Parameters
button –
QAbstractButton
- Return type
Returns the button role for the specified
button
. This function returnsInvalidRole
ifbutton
is 0 or has not been added to the message box.See also
- Parameters
button –
int
- Return type
unicode
Returns the text of the message box button
button
, or an empty string if the message box does not contain the button.Use
button()
andtext()
instead.See also
- Return type
Returns a list of all the buttons that have been added to the message box.
See also
-
PySide2.QtWidgets.QMessageBox.
checkBox
()¶ - Return type
Returns the checkbox shown on the dialog. This is 0 if no checkbox is set.
See also
-
PySide2.QtWidgets.QMessageBox.
clickedButton
()¶ - Return type
Returns the button that was clicked by the user, or 0 if the user hit the Esc key and no
escape button
was set.If
exec()
hasn’t been called yet, returns nullptr.Example:
messageBox = QMessageBox(self) disconnectButton = messageBox.addButton(self.tr("Disconnect"), QMessageBox.ActionRole) ... messageBox.exec_() if messageBox.clickedButton() == disconnectButton: ...
See also
-
static
PySide2.QtWidgets.QMessageBox.
critical
(parent, title, text, button0, button1)¶ - Parameters
parent –
QWidget
title – unicode
text – unicode
button0 –
StandardButton
button1 –
StandardButton
- Return type
int
-
static
PySide2.QtWidgets.QMessageBox.
critical
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]]) - Parameters
parent –
QWidget
title – unicode
text – unicode
buttons –
StandardButtons
defaultButton –
StandardButton
- Return type
-
PySide2.QtWidgets.QMessageBox.
defaultButton
()¶ - Return type
Returns the button that should be the message box’s
default button
. Returns nullptr if no default button was set.See also
-
PySide2.QtWidgets.QMessageBox.
detailedText
()¶ - Return type
unicode
See also
-
PySide2.QtWidgets.QMessageBox.
escapeButton
()¶ - Return type
Returns the button that is activated when escape is pressed.
By default,
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.
See also
-
PySide2.QtWidgets.QMessageBox.
iconPixmap
()¶ - Return type
QPixmap
See also
-
static
PySide2.QtWidgets.QMessageBox.
information
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])¶ - Parameters
parent –
QWidget
title – unicode
text – unicode
buttons –
StandardButtons
defaultButton –
StandardButton
- Return type
-
static
PySide2.QtWidgets.QMessageBox.
information
(parent, title, text, button0[, button1=NoButton]) - Parameters
parent –
QWidget
title – unicode
text – unicode
button0 –
StandardButton
button1 –
StandardButton
- Return type
-
PySide2.QtWidgets.QMessageBox.
informativeText
()¶ - Return type
unicode
See also
-
PySide2.QtWidgets.QMessageBox.
open
(receiver, member)¶ - Parameters
receiver –
QObject
member – str
Opens the dialog and connects its
finished()
orbuttonClicked()
signal to the slot specified byreceiver
andmember
. If the slot inmember
has a pointer for its first parameter the connection is tobuttonClicked()
, otherwise the connection is tofinished()
.The signal will be disconnected from the slot when the dialog is closed.
-
static
PySide2.QtWidgets.QMessageBox.
question
(parent, title, text[, buttons=QMessageBox.StandardButtons(Yes | No)[, defaultButton=NoButton]])¶ - Parameters
parent –
QWidget
title – unicode
text – unicode
buttons –
StandardButtons
defaultButton –
StandardButton
- Return type
-
static
PySide2.QtWidgets.QMessageBox.
question
(parent, title, text, button0, button1) - Parameters
parent –
QWidget
title – unicode
text – unicode
button0 –
StandardButton
button1 –
StandardButton
- Return type
int
-
PySide2.QtWidgets.QMessageBox.
removeButton
(button)¶ - Parameters
button –
QAbstractButton
Removes
button
from the button box without deleting it.See also
-
PySide2.QtWidgets.QMessageBox.
setButtonText
(button, text)¶ - Parameters
button –
int
text – unicode
Sets the text of the message box button
button
totext
. Setting the text of a button that is not in the message box is silently ignored.Use
addButton()
instead.See also
-
PySide2.QtWidgets.QMessageBox.
setCheckBox
(cb)¶ - Parameters
cb –
QCheckBox
Sets the checkbox
cb
on the message dialog. The message box takes ownership of the checkbox. The argumentcb
can be 0 to remove an existing checkbox from the message box.See also
-
PySide2.QtWidgets.QMessageBox.
setDefaultButton
(button)¶ - Parameters
button –
StandardButton
Sets the message box’s
default button
tobutton
.See also
-
PySide2.QtWidgets.QMessageBox.
setDefaultButton
(button) - Parameters
button –
QPushButton
Sets the message box’s
default button
tobutton
.See also
-
PySide2.QtWidgets.QMessageBox.
setDetailedText
(text)¶ - Parameters
text – unicode
See also
-
PySide2.QtWidgets.QMessageBox.
setEscapeButton
(button)¶ - Parameters
button –
QAbstractButton
Sets the button that gets activated when the Escape key is pressed to
button
.See also
-
PySide2.QtWidgets.QMessageBox.
setEscapeButton
(button) - Parameters
button –
StandardButton
Sets the buttons that gets activated when the Escape key is pressed to
button
.See also
-
PySide2.QtWidgets.QMessageBox.
setIconPixmap
(pixmap)¶ - Parameters
pixmap –
QPixmap
See also
-
PySide2.QtWidgets.QMessageBox.
setInformativeText
(text)¶ - Parameters
text – unicode
See also
-
PySide2.QtWidgets.QMessageBox.
setStandardButtons
(buttons)¶ - Parameters
buttons –
StandardButtons
See also
-
PySide2.QtWidgets.QMessageBox.
setTextFormat
(format)¶ - Parameters
format –
TextFormat
See also
-
PySide2.QtWidgets.QMessageBox.
setTextInteractionFlags
(flags)¶ - Parameters
flags –
TextInteractionFlags
See also
-
PySide2.QtWidgets.QMessageBox.
standardButton
(button)¶ - Parameters
button –
QAbstractButton
- Return type
Returns the standard button enum value corresponding to the given
button
, orNoButton
if the givenbutton
isn’t a standard button.See also
-
PySide2.QtWidgets.QMessageBox.
standardButtons
()¶ - Return type
StandardButtons
See also
-
static
PySide2.QtWidgets.QMessageBox.
standardIcon
(icon)¶ - Parameters
icon –
Icon
- Return type
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.Question
,Information
,Warning
orCritical
.Call
standardIcon()
withSP_MessageBoxInformation
etc. instead.
-
PySide2.QtWidgets.QMessageBox.
textFormat
()¶ - Return type
TextFormat
See also
-
PySide2.QtWidgets.QMessageBox.
textInteractionFlags
()¶ - Return type
TextInteractionFlags
See also
-
static
PySide2.QtWidgets.QMessageBox.
warning
(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])¶ - Parameters
parent –
QWidget
title – unicode
text – unicode
buttons –
StandardButtons
defaultButton –
StandardButton
- Return type
-
static
PySide2.QtWidgets.QMessageBox.
warning
(parent, title, text, button0, button1) - Parameters
parent –
QWidget
title – unicode
text – unicode
button0 –
StandardButton
button1 –
StandardButton
- Return type
int
© 2018 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.