Home · All Classes · Modules

QMessageBox Class Reference
[QtGui module]

The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style. More...

Inherits QDialog.

Types

Methods

Static Methods


Detailed Description

The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style.

Message boxes are used to provide informative messages and to ask simple questions.

Basic Usage

The easiest way to pop up a message box in Qt is to call one of the static functions QMessageBox.information(), QMessageBox.question(), QMessageBox.critical(), and QMessageBox.warning(). For example:

         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);

Buttons are specified by combining StandardButtons using the bitwise OR operator. The order of the buttons on screen is platform-dependent. For example, on Windows, Save is displayed to the left of Cancel, whereas on Mac OS, the order is reversed.

The text part of all message box messages can be either rich text or plain text. With certain strings that contain XML meta characters, the auto-rich text detection may fail, interpreting plain text incorrectly as rich text. In these rare cases, use Qt.convertFromPlainText() to convert your plain text string to a visually equivalent rich text string or set the text format explicitly with setTextFormat().

Note that the Microsoft Windows User Interface Guidelines recommend using the application name as the window's title.

The Standard Dialogs example shows how to use QMessageBox as well as other built-in Qt dialogs.

Severity Levels

QMessageBox supports four severity levels, indicated by an icon:

QuestionFor message boxes that ask a question as part of normal operation. Some style guides recommend using Information for this purpose.

InformationFor message boxes that are part of normal operation.

WarningFor message boxes that tell the user about unusual errors.

CriticalFor message boxes that tell the user about critical errors.

Advanced Usage

If the convenience static functions, such as QMessageBox.information() and QMessageBox.warning(), are not flexible enough for your needs, you can instantiate a QMessageBox on the stack. You can then use addButton() to add buttons with standard or arbitrary text.

When using an instance of QMessageBox with standard buttons, you can test the return value of exec_() to determine which button was clicked. For example,

         QMessageBox msgBox;
         msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No);
         switch (msgBox.exec()) {
         case QMessageBox.Yes:
             // yes was clicked
             break;
         case QMessageBox.No:
             // no was clicked
             break;
         default:
             // should never be reached
             break;
         }

When using an instance of QMessageBox with custom buttons, you can test the value of clickedButton() after calling exec_(). For example,

         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
         }

In the example above, the Connect button is created using the addButton() overload that takes a text and a ButtonRole. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform).

The text(), icon() and iconPixmap() functions provide access to the current text and pixmap of the message box. The setText(), setIcon() and setIconPixmap() let you change it. The difference between setIcon() and setIconPixmap() is that the former accepts a QMessageBox.Icon and can be used to set standard icons, whereas the latter accepts a QPixmap and can be used to set custom icons.

setButtonText() and buttonText() provide access to the buttons.

Default and Escape Keys

The default button (i.e., the button that is activated when the user presses Enter) can be specified using setDefaultButton(). If none is specified, QMessageBox will try to find one automatically based on the ButtonRoles of the buttons in the dialog.

Similarly, the escape button (the button that is activated when the user presses Esc) is specified using setEscapeButton(). If no escape button is specified, QMessageBox attempts to automatically detect an escape button as follows:

  1. If there is only one button, it is made the escape button.
  2. If there is a Cancel button, it is made the escape button.
  3. If there is exactly one button with the role QMessageBox.RejectRole or QMessageBox.NoRole, it is made the escape button.

When an escape button could not be automatically detected, pressing Esc has no effect.

Notes for X11 and Mac

The message dialogs will show a close button on X11 and Mac. However, clicking it will not close the dialog. This is because there is no mechanism on these platforms to disable the close button.

See also QDialogButtonBox, GUI Design Handbook: Message Box, Standard Dialogs Example, and Application Example.


Type Documentation

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.

ConstantValueDescription
QMessageBox.InvalidRole-1The button is invalid.
QMessageBox.AcceptRole0Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox.RejectRole1Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox.DestructiveRole2Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox.ActionRole3Clicking the button causes changes to the elements within the dialog.
QMessageBox.HelpRole4The button can be clicked to request help.
QMessageBox.YesRole5The button is a "Yes"-like button.
QMessageBox.NoRole6The button is a "No"-like button.
QMessageBox.ApplyRole8The button applies current changes.
QMessageBox.ResetRole7The button resets the dialog's fields to default values.

See also StandardButton.

QMessageBox.Icon

This enum has the following values:

ConstantValueDescription
QMessageBox.NoIcon0the message box does not have any icon.
QMessageBox.Question4an icon indicating that the message is asking a question.
QMessageBox.Information1an icon indicating that the message is nothing out of the ordinary.
QMessageBox.Warning2an icon indicating that the message is a warning, but can be dealt with.
QMessageBox.Critical3an icon indicating that the message represents a critical problem.

QMessageBox.StandardButton

These enums describe flags for standard buttons. Each button has a defined ButtonRole.

ConstantValueDescription
QMessageBox.Ok0x00000400An "OK" button defined with the AcceptRole.
QMessageBox.Open0x00002000A "Open" button defined with the AcceptRole.
QMessageBox.Save0x00000800A "Save" button defined with the AcceptRole.
QMessageBox.Cancel0x00400000A "Cancel" button defined with the RejectRole.
QMessageBox.Close0x00200000A "Close" button defined with the RejectRole.
QMessageBox.Discard0x00800000A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QMessageBox.Apply0x02000000An "Apply" button defined with the ApplyRole.
QMessageBox.Reset0x04000000A "Reset" button defined with the ResetRole.
QMessageBox.RestoreDefaults0x08000000A "Restore Defaults" button defined with the ResetRole.
QMessageBox.Help0x01000000A "Help" button defined with the HelpRole.
QMessageBox.SaveAll0x00001000A "Save All" button defined with the AcceptRole.
QMessageBox.Yes0x00004000A "Yes" button defined with the YesRole.
QMessageBox.YesToAll0x00008000A "Yes to All" button defined with the YesRole.
QMessageBox.No0x00010000A "No" button defined with the NoRole.
QMessageBox.NoToAll0x00020000A "No to All" button defined with the NoRole.
QMessageBox.Abort0x00040000An "Abort" button defined with the RejectRole.
QMessageBox.Retry0x00080000A "Retry" button defined with the AcceptRole.
QMessageBox.Ignore0x00100000An "Ignore" button defined with the AcceptRole.
QMessageBox.NoButton0x00000000An invalid button.

The following values are obsolete:

ConstantValueDescription
QMessageBox.YesAllYesToAllUse YesToAll instead.
QMessageBox.NoAllNoToAllUse NoToAll instead.
QMessageBox.Default0x00000100Use the defaultButton argument of information(), warning(), etc. instead, or call setDefaultButton().
QMessageBox.Escape0x00000200Call setEscapeButton() instead.
QMessageBox.FlagMask0x00000300 
QMessageBox.ButtonMask~FlagMask 

This enum was introduced in Qt 4.2.

The StandardButtons type is a typedef for QFlags<StandardButton>. It stores an OR combination of StandardButton values.

See also ButtonRole and standardButtons.


Method Documentation

QMessageBox.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a message box with no text and no buttons.

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

The parent argument is passed to the QDialog constructor.

QMessageBox.__init__ (self, Icon icon, QString title, QString text, StandardButtons buttons = QMessageBox.NoButton, QWidget parent = None, Qt.WindowFlags f = Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a message box with the given icon, title, text, and standard buttons. (Buttons can also be added at any time using addButton().)

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

The parent and f arguments are passed to the QDialog constructor.

See also setWindowTitle(), setText(), setIcon(), and setStandardButtons().

QMessageBox.__init__ (self, QString title, QString text, Icon icon, int button0, int button1, int button2, QWidget parent = None, Qt.WindowFlags f = Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

QMessageBox.about (QWidget parent, QString caption, QString text)

Displays a simple about box with title title and text text. The about box's parent is parent.

about() looks for a suitable icon in four locations:

  1. It prefers parent->icon() if that exists.
  2. If not, it tries the top-level widget containing parent.
  3. If that fails, it tries the active window.
  4. As a last resort it uses the Information icon.

The about box has a single button labelled "OK".

See also QWidget.windowIcon() and QApplication.activeWindow().

QMessageBox.aboutQt (QWidget parent, QString caption = QString())

Displays a simple message box about Qt, with the given title and centered over parent (if parent 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.

See also QApplication.aboutQt().

QMessageBox.addButton (self, QAbstractButton button, ButtonRole role)

The button argument has it's ownership transferred to Qt.

Adds the given button to the message box with the specified role.

This function was introduced in Qt 4.2.

See also removeButton(), button(), and setStandardButtons().

QPushButton QMessageBox.addButton (self, QString text, ButtonRole role)

This is an overloaded member function, provided for convenience.

Creates a button with the given text, adds it to the message box for the specified role, and returns it.

This function was introduced in Qt 4.2.

QPushButton QMessageBox.addButton (self, StandardButton button)

This is an overloaded member function, provided for convenience.

Adds a standard button to the message box if it is valid to do so, and returns the push button.

This function was introduced in Qt 4.2.

See also setStandardButtons().

QAbstractButton QMessageBox.button (self, StandardButton which)

Returns a pointer corresponding to the standard button which, or 0 if the standard button doesn't exist in this message box.

This function was introduced in Qt 4.2.

See also standardButtons and standardButton().

QString QMessageBox.buttonText (self, int button)

QMessageBox.changeEvent (self, QEvent)

QAbstractButton QMessageBox.clickedButton (self)

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

Example:

         QMessageBox messageBox(this);
         QAbstractButton *disconnectButton =
               messageBox.addButton(tr("Disconnect"), QMessageBox.ActionRole);
         ...
         messageBox.exec();
         if (messageBox.clickedButton() == disconnectButton) {
             ...
         }

This function was introduced in Qt 4.2.

See also standardButton() and button().

QMessageBox.closeEvent (self, QCloseEvent)

StandardButton QMessageBox.critical (QWidget parent, QString title, QString text, StandardButtons buttons = QMessageBox.Ok, StandardButton defaultButton = QMessageBox.NoButton)

Opens a critical message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox.NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

This function was introduced in Qt 4.2.

See also question(), warning(), and information().

int QMessageBox.critical (QWidget parent, QString caption, QString text, int button0, int button1, int button2 = 0)

int QMessageBox.critical (QWidget parent, QString title, QString text, QString button0Text, QString button1Text = QString(), QString button2Text = QString(), int defaultButtonNumber = 0, int escapeButtonNumber = -1)

QPushButton QMessageBox.defaultButton (self)

Returns the button that should be the message box's default button. Returns 0 if no default button was set.

This function was introduced in Qt 4.2.

See also setDefaultButton(), addButton(), and QPushButton.setDefault().

QString QMessageBox.detailedText (self)

QAbstractButton QMessageBox.escapeButton (self)

Returns the button that is activated when escape is pressed.

By default, QMessageBox attempts to automatically detect an escape button as follows:

  1. If there is only one button, it is made the escape button.
  2. If there is a Cancel button, it is made the escape button.
  3. On Mac OS X only, if there is exactly one button with the role QMessageBox.RejectRole, it is made the escape button.

When an escape button could not be automatically detected, pressing Esc has no effect.

This function was introduced in Qt 4.2.

See also setEscapeButton() and addButton().

bool QMessageBox.event (self, QEvent e)

Icon QMessageBox.icon (self)

QPixmap QMessageBox.iconPixmap (self)

StandardButton QMessageBox.information (QWidget parent, QString title, QString text, StandardButtons buttons = QMessageBox.Ok, StandardButton defaultButton = QMessageBox.NoButton)

Opens an information message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox.NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

This function was introduced in Qt 4.2.

See also question(), warning(), and critical().

int QMessageBox.information (QWidget parent, QString caption, QString text, int button0, int button1 = 0, int button2 = 0)

int QMessageBox.information (QWidget parent, QString title, QString text, QString button0Text, QString button1Text = QString(), QString button2Text = QString(), int defaultButtonNumber = 0, int escapeButtonNumber = -1)

QString QMessageBox.informativeText (self)

QMessageBox.keyPressEvent (self, QKeyEvent)

StandardButton QMessageBox.question (QWidget parent, QString title, QString text, StandardButtons buttons = QMessageBox.Ok, StandardButton defaultButton = QMessageBox.NoButton)

Opens a question message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox.NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

This function was introduced in Qt 4.2.

See also information(), warning(), and critical().

int QMessageBox.question (QWidget parent, QString caption, QString text, int button0, int button1 = 0, int button2 = 0)

int QMessageBox.question (QWidget parent, QString title, QString text, QString button0Text, QString button1Text = QString(), QString button2Text = QString(), int defaultButtonNumber = 0, int escapeButtonNumber = -1)

QMessageBox.removeButton (self, QAbstractButton button)

The button argument

Removes button from the button box without deleting it.

This function was introduced in Qt 4.2.

See also addButton() and setStandardButtons().

QMessageBox.resizeEvent (self, QResizeEvent)

QMessageBox.setButtonText (self, int button, QString)

QMessageBox.setDefaultButton (self, QPushButton button)

Sets the message box's default button to button.

This function was introduced in Qt 4.2.

See also defaultButton(), addButton(), and QPushButton.setDefault().

QMessageBox.setDefaultButton (self, StandardButton button)

This is an overloaded member function, provided for convenience.

Sets the message box's default button to button.

This function was introduced in Qt 4.3.

See also addButton() and QPushButton.setDefault().

QMessageBox.setDetailedText (self, QString text)

QMessageBox.setEscapeButton (self, QAbstractButton button)

Sets the button that gets activated when the Escape key is pressed to button.

This function was introduced in Qt 4.2.

See also escapeButton(), addButton(), and clickedButton().

QMessageBox.setEscapeButton (self, StandardButton button)

This is an overloaded member function, provided for convenience.

Sets the buttons that gets activated when the Escape key is pressed to button.

This function was introduced in Qt 4.3.

See also addButton() and clickedButton().

QMessageBox.setIcon (self, Icon)

QMessageBox.setIconPixmap (self, QPixmap)

QMessageBox.setInformativeText (self, QString text)

QMessageBox.setStandardButtons (self, StandardButtons buttons)

QMessageBox.setText (self, QString)

QMessageBox.setTextFormat (self, Qt.TextFormat)

QMessageBox.setWindowModality (self, Qt.WindowModality windowModality)

This function shadows QWidget.setWindowModality().

Sets the modality of the message box to windowModality.

On Mac OS X, if the modality is set to Qt.WindowModal and the message box has a parent, then the message box will be a Qt.Sheet, otherwise the message box will be a standard dialog.

This function was introduced in Qt 4.2.

QMessageBox.setWindowTitle (self, QString title)

This function shadows QWidget.setWindowTitle().

Sets the title of the message box to title. On Mac OS X, the window title is ignored (as required by the Mac OS X Guidelines).

This function was introduced in Qt 4.2.

QMessageBox.showEvent (self, QShowEvent)

QSize QMessageBox.sizeHint (self)

StandardButton QMessageBox.standardButton (self, QAbstractButton button)

Returns the standard button enum value corresponding to the given button, or NoButton if the given button isn't a standard button.

This function was introduced in Qt 4.2.

See also button() and standardButtons().

StandardButtons QMessageBox.standardButtons (self)

QPixmap QMessageBox.standardIcon (Icon icon)

QString QMessageBox.text (self)

Qt.TextFormat QMessageBox.textFormat (self)

StandardButton QMessageBox.warning (QWidget parent, QString title, QString text, StandardButtons buttons = QMessageBox.Ok, StandardButton defaultButton = QMessageBox.NoButton)

Opens a warning message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox.NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

This function was introduced in Qt 4.2.

See also question(), information(), and critical().

int QMessageBox.warning (QWidget parent, QString caption, QString text, int button0, int button1, int button2 = 0)

int QMessageBox.warning (QWidget parent, QString title, QString text, QString button0Text, QString button1Text = QString(), QString button2Text = QString(), int defaultButtonNumber = 0, int escapeButtonNumber = -1)


PyQt 4.4.3 for X11Copyright © Riverbank Computing Ltd and Trolltech AS 2008Qt 4.4.1