QMessageBox Class Reference

The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.

  1. #include <QMessageBox>

Inherits: QDialog.

Detailed Description

The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.

A message box displays a primary text to alert the user to a situation, an informative text to further explain the alert or to ask the user a question, and an optional detailed text to provide even more data if the user requests it. A message box can also display an icon and standard 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 call exec() to show the message. The simplest configuration is to set only the message text property.

  1. QMessageBox msgBox;
  2. msgBox.setText("The document has been modified.");
  3. 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 the standard buttons property to the set of buttons you want as the set of user responses. The buttons are specified by combining values from StandardButtons 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.

  1. QMessageBox msgBox;
  2. msgBox.setText("The document has been modified.");
  3. msgBox.setInformativeText("Do you want to save your changes?");
  4. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  5. msgBox.setDefaultButton(QMessageBox::Save);
  6. int ret = msgBox.exec();

This is the approach recommended in the Mac OS X Guidlines. Similar guidlines apply for the other platforms, but note the different ways the informative text is handled for different platforms.

The exec() slot returns the StandardButtons value of the button that was clicked.

  1. switch (ret)  {
  2.   case QMessageBox::Save:
  3.       // Save was clicked
  4.       break;
  5.   case QMessageBox::Discard:
  6.       // Don't Save was clicked
  7.       break;
  8.   case QMessageBox::Cancel:
  9.       // Cancel was clicked
  10.       break;
  11.   default:
  12.       // should never be reached
  13.       break;
  14. }

To give the user more information to help him answer the question, set the detailed text property. If the detailed 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. The main text and informative text properties can be either plain text or rich text. These strings are interpreted according to the setting of the text format property. The default setting is auto-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, use Qt::convertFromPlainText() to convert your plain text string to a visually equivalent rich text string, or set the text format property explicitly with setTextFormat().

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 the icon property to one of the predefined icons. The following rules are guidelines:

QuestionFor asking a question during normal operations.
InformationFor reporting information about normal operations.
WarningFor reporting non-critical errors.
CriticalFor reporting critical errors.

Predefined icons are not defined by QMessageBox, but provided by the style. The default value is No 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 the icon pixmap property instead of setting the icon property.

In summary, to set an icon, use either setIcon() for one of the standard icons, or setIconPixmap() 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 and detailed text properties. One work-around for this has been to use the title parameter as the message box main text and the text 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 the application name as the window's title, which means that if you have an informative text in addition to your main text, you must concatenate it to the text 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 the default button.

Static functions are available for creating information(), question(), warning(), and critical() message boxes.

  1. int ret = QMessageBox::warning(this, tr("My Application"),
  2.                                tr("The document has been modified.\n"
  3.                                   "Do you want to save your changes?"),
  4.                                QMessageBox::Save | QMessageBox::Discard
  5.                                | QMessageBox::Cancel,
  6.                                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 the addButton() overload that takes a text and a ButtonRoleto to add custom buttons. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform). You can test the value of clickedButton() after calling exec(). For example,

  1. QMessageBox msgBox;
  2. QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
  3. QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);
  4.  
  5. msgBox.exec();
  6.  
  7. if (msgBox.clickedButton() == connectButton)  {
  8.     // connect
  9. } else if (msgBox.clickedButton() == abortButton)  {
  10.     // abort
  11. }

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 the button 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:

  1. If there is only one button, it is the button activated when Esc is pressed.
  2. If there is a Cancel button, it is the button activated when Esc is pressed.
  3. If there is exactly one button having either the Reject role or the 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.

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

Public Types

Toggle detailsenum QMessageBox::

ButtonRoleButtonRole { InvalidRole , AcceptRole , RejectRole , DestructiveRole , ActionRole , HelpRole , YesRole , NoRole , ResetRole , ApplyRole , NRoles 9 ...} { InvalidRole , AcceptRole , RejectRole , DestructiveRole , ActionRole , HelpRole , YesRole , NoRole , ResetRole , ApplyRole , NRoles 9 }

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 -1 The button is invalid.
QMessageBox::AcceptRole 0 Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole 1 Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole 2 Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox::ActionRole 3 Clicking the button causes changes to the elements within the dialog.
QMessageBox::HelpRole 4 The button can be clicked to request help.
QMessageBox::YesRole 5 The button is a "Yes"-like button.
QMessageBox::NoRole 6 The button is a "No"-like button.
QMessageBox::ApplyRole 8 The button applies current changes.
QMessageBox::ResetRole 7 The button resets the dialog's fields to default values.

See also StandardButton.

Look up this member in the source code.

Toggle detailsenum QMessageBox::

IconIcon { NoIcon , Information , Warning , Critical , Question 4 ...} { NoIcon , Information , Warning , Critical , Question 4 }

This enum has the following values:

ConstantValueDescription
QMessageBox::NoIcon 0 the message box does not have any icon.
QMessageBox::Question 4 an icon indicating that the message is asking a question.
QMessageBox::Information 1 an icon indicating that the message is nothing out of the ordinary.
QMessageBox::Warning 2 an icon indicating that the message is a warning, but can be dealt with.
QMessageBox::Critical 3 an icon indicating that the message represents a critical problem.

Look up this member in the source code.

Toggle detailsenum QMessageBox::

StandardButtonStandardButton { NoButton , Ok , Save , SaveAll , Open , Yes , YesToAll , No , NoToAll , Abort , Retry , Ignore , Close , Cancel , Discard , Help , Apply , Reset , RestoreDefaults , FirstButton , LastButton , YesAll , NoAll , Default , Escape , FlagMask , ButtonMask ~FlagMask ...} { NoButton , Ok , Save , SaveAll , Open , Yes , YesToAll , No , NoToAll , Abort , Retry , Ignore , Close , Cancel , Discard , Help , Apply , Reset , RestoreDefaults , FirstButton , LastButton , YesAll , NoAll , Default , Escape , FlagMask , ButtonMask ~FlagMask }

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

ConstantValueDescription
QMessageBox::Ok 0x00000400 An "OK" button defined with the AcceptRole.
QMessageBox::Open 0x00002000 A "Open" button defined with the AcceptRole.
QMessageBox::Save 0x00000800 A "Save" button defined with the AcceptRole.
QMessageBox::Cancel 0x00400000 A "Cancel" button defined with the RejectRole.
QMessageBox::Close 0x00200000 A "Close" button defined with the RejectRole.
QMessageBox::Discard 0x00800000 A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QMessageBox::Apply 0x02000000 An "Apply" button defined with the ApplyRole.
QMessageBox::Reset 0x04000000 A "Reset" button defined with the ResetRole.
QMessageBox::RestoreDefaults 0x08000000 A "Restore Defaults" button defined with the ResetRole.
QMessageBox::Help 0x01000000 A "Help" button defined with the HelpRole.
QMessageBox::SaveAll 0x00001000 A "Save All" button defined with the AcceptRole.
QMessageBox::Yes 0x00004000 A "Yes" button defined with the YesRole.
QMessageBox::YesToAll 0x00008000 A "Yes to All" button defined with the YesRole.
QMessageBox::No 0x00010000 A "No" button defined with the NoRole.
QMessageBox::NoToAll 0x00020000 A "No to All" button defined with the NoRole.
QMessageBox::Abort 0x00040000 An "Abort" button defined with the RejectRole.
QMessageBox::Retry 0x00080000 A "Retry" button defined with the AcceptRole.
QMessageBox::Ignore 0x00100000 An "Ignore" button defined with the AcceptRole.
QMessageBox::NoButton 0x00000000 An invalid button.

The following values are obsolete:

ConstantValueDescription
QMessageBox::YesAll YesToAll Use YesToAll instead.
QMessageBox::NoAll NoToAll Use NoToAll instead.
QMessageBox::Default 0x00000100 Use the defaultButton argument of information(), warning(), etc. instead, or call setDefaultButton().
QMessageBox::Escape 0x00000200 Call setEscapeButton() instead.
QMessageBox::FlagMask 0x00000300
QMessageBox::ButtonMask ~FlagMask

See also ButtonRole and standardButtons.

Look up this member in the source code.

    Properties

    Toggle detailsdetailedTextdetailedText : QString

    This property holds the text to be displayed in the details area.

    The text will be interpreted as a plain text.

    By default, this property contains an empty string.

    See also QMessageBox::text and QMessageBox::informativeText.

    Access functions:

    Look up this member in the source code.

    Toggle detailsiconicon : Icon

    This property holds the message box's icon

    The icon of the message box can be specified with one of the values:

    The default is QMessageBox::NoIcon.

    The pixmap used to display the actual icon depends on the current GUI style. You can also set a custom pixmap for the icon by setting the icon pixmap property.

    See also iconPixmap.

    Access functions:

    Look up this member in the source code.

    Toggle detailsiconPixmapiconPixmap : QPixmap

    This property holds the current icon

    The icon currently used by the message box. Note that it's often hard to draw one pixmap that looks appropriate in all GUI styles; you may want to supply a different pixmap for each platform.

    By default, this property is undefined.

    See also icon.

    Access functions:

    Look up this member in the source code.

    Toggle detailsinformativeTextinformativeText : QString

    This property holds the informative text that provides a fuller description for the message

    Infromative text can be used to expand upon the text() to give more information to the user. On the Mac, this text appears in small system font below the text(). On other platforms, it is simply appended to the existing text.

    By default, this property contains an empty string.

    See also QMessageBox::text and QMessageBox::detailedText.

    Access functions:

    Look up this member in the source code.

    Toggle detailsstandardButtonsstandardButtons : StandardButtons

    This property holds collection of standard buttons in the message box

    This property controls which standard buttons are used by the message box.

    By default, this property contains no standard buttons.

    See also addButton().

    Access functions:

    Look up this member in the source code.

    Toggle detailstexttext : QString

    This property holds the message box text to be displayed.

    The text will be interpreted either as a plain text or as rich text, depending on the text format setting (QMessageBox::textFormat). The default setting is Qt::AutoText, i.e., the message box will try to auto-detect the format of the text.

    The default value of this property is an empty string.

    See also textFormat, QMessageBox::informativeText, and QMessageBox::detailedText.

    Access functions:

    • QString text () const
    • void setText ( const QString & text )

    Look up this member in the source code.

    Toggle detailstextFormattextFormat : Qt::TextFormat

    This property holds the format of the text displayed by the message box

    The current text format used by the message box. See the Qt::TextFormat enum for an explanation of the possible options.

    The default format is Qt::AutoText.

    See also setText().

    Access functions:

    Look up this member in the source code.

      Public Functions

      Toggle details QMessageBox

      QMessageBoxQMessageBox ( QWidget *parent=0 ) ( QWidget *parent=0 )

      Constructs a message box with no text and no buttons. parent is passed to the QDialog constructor.

      On Mac OS X, if you want your message box to appear as a Qt::Sheet of its parent, set the message box's window modality to Qt::WindowModal or use open(). Otherwise, the message box will be a standard dialog.

      Look up this member in the source code.

      Toggle details QMessageBox

      QMessageBoxQMessageBox ( Icon icon , const QString &title , const QString &text , StandardButtons buttons=NoButton , QWidget *parent=0 , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ...) ( Icon icon , const QString &title , const QString &text , StandardButtons buttons=NoButton , QWidget *parent=0 , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint )

      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(). The parent and f arguments are passed to the QDialog constructor.

      The message box is an application modal dialog box.

      On Mac OS X, if parent is not 0 and you want your message box to appear as a Qt::Sheet of that parent, set the message box's window modality to Qt::WindowModal (default). Otherwise, the message box will be a standard dialog.

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

      Look up this member in the source code.

      Toggle details QMessageBox

      ~QMessageBox~QMessageBox () ()

      Destroys the message box.

      Look up this member in the source code.

      Toggle details void QMessageBox

      aboutabout ( QWidget *parent , const QString &title , const QString &text ...) ( QWidget *parent , const QString &title , const QString &text ) [static]

      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". On Mac OS X, the about box is popped up as a modeless window; on other platforms, it is currently application modal.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      aboutQtaboutQt ( QWidget *parent , const QString &title=QString() ...) ( QWidget *parent , const QString &title=QString() ) [static]

      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.

      On Mac OS X, the about box is popped up as a modeless window; on other platforms, it is currently application modal.

      See also QApplication::aboutQt().

      Look up this member in the source code.

      Toggle details void QMessageBox

      addButtonaddButton ( QAbstractButton *button , ButtonRole role ...) ( QAbstractButton *button , ButtonRole role )

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

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

      Look up this member in the source code.

      Toggle details QPushButton * QMessageBox

      addButtonaddButton ( const QString &text , ButtonRole role ...) ( const QString &text , ButtonRole role )

      This is an overloaded function.

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

      Look up this member in the source code.

      Toggle details QPushButton * QMessageBox

      addButtonaddButton ( StandardButton button ) ( StandardButton button )

      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 setStandardButtons().

      Look up this member in the source code.

      Toggle details QAbstractButton * QMessageBox

      buttonbutton ( StandardButton which ) ( StandardButton which )const

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

      See also standardButtons and standardButton().

      Look up this member in the source code.

      Toggle details ButtonRole QMessageBox

      buttonRolebuttonRole ( QAbstractButton *button ) ( QAbstractButton *button )const

      Returns the button role for the specified button. This function returns InvalidRole if button is 0 or has not been added to the message box.

      See also buttons() and addButton().

      Look up this member in the source code.

      Toggle details QList <QAbstractButton *> QMessageBox

      buttonsbuttons () ()const

      Returns a list of all the buttons that have been added to the message box.

      See also buttonRole(), addButton(), and removeButton().

      Look up this member in the source code.

      Toggle details void QMessageBox

      changeEventchangeEvent ( QEvent *ev ) ( QEvent *ev ) [virtual protected]

      Reimplemented from QWidget::changeEvent().

      Look up this member in the source code.

      Toggle details QAbstractButton * QMessageBox

      clickedButtonclickedButton () ()const

      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:

      1.                     QMessageBox messageBox(this);
      2. QAbstractButton *disconnectButton =
      3.       messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
      4. ...
      5. messageBox.exec();
      6. if (messageBox.clickedButton() == disconnectButton)  {
      7.     ...
      8. }

      See also standardButton() and button().

      Look up this member in the source code.

      Toggle details void QMessageBox

      closeEventcloseEvent ( QCloseEvent *e ) ( QCloseEvent *e ) [virtual protected]

      Reimplemented from QWidget::closeEvent().

      Look up this member in the source code.

      Toggle details StandardButton QMessageBox

      criticalcritical ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ...) ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ) [static]

      Opens a critical message box with the given title and text in front of the specified parent widget.

      The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons. If defaultButton is QMessageBox::NoButton, QMessageBox chooses a suitable default automatically.

      Returns the identity of the standard button that was clicked. If Esc was pressed instead, the escape button is returned.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details QPushButton * QMessageBox

      defaultButtondefaultButton () ()const

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

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

      Look up this member in the source code.

      Toggle details QAbstractButton * QMessageBox

      escapeButtonescapeButton () ()const

      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.

      See also setEscapeButton() and addButton().

      Look up this member in the source code.

      Toggle details bool QMessageBox

      eventevent ( QEvent *e ) ( QEvent *e ) [virtual protected]

      Reimplemented from QObject::event().

      Look up this member in the source code.

      Toggle details StandardButton QMessageBox

      informationinformation ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ...) ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ) [static]

      Opens an information message box with the given title and text in front of the specified parent widget.

      The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons. If defaultButton is QMessageBox::NoButton, QMessageBox chooses a suitable default automatically.

      Returns the identity of the standard button that was clicked. If Esc was pressed instead, the escape button is returned.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      keyPressEventkeyPressEvent ( QKeyEvent *e ) ( QKeyEvent *e ) [virtual protected]

      Reimplemented from QWidget::keyPressEvent().

      Look up this member in the source code.

      Toggle details void QMessageBox

      openopen ( QObject *receiver , const char *member ) ( QObject *receiver , const char *member )

      This is an overloaded function.

      Opens the dialog and connects its finished() or buttonClicked() signal to the slot specified by receiver and member. If the slot in member has a pointer for its first parameter the connection is to buttonClicked(), otherwise the connection is to finished().

      The signal will be disconnected from the slot when the dialog is closed.

      Look up this member in the source code.

      Toggle details StandardButton QMessageBox

      questionquestion ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ...) ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ) [static]

      Opens a question message box with the given title and text in front of the specified parent widget.

      The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons. If defaultButton is QMessageBox::NoButton, QMessageBox chooses a suitable default automatically.

      Returns the identity of the standard button that was clicked. If Esc was pressed instead, the escape button is returned.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      removeButtonremoveButton ( QAbstractButton *button ) ( QAbstractButton *button )

      Removes button from the button box without deleting it.

      See also addButton() and setStandardButtons().

      Look up this member in the source code.

      Toggle details void QMessageBox

      resizeEventresizeEvent ( QResizeEvent *event ) ( QResizeEvent *event ) [virtual protected]

      Reimplemented from QWidget::resizeEvent().

      Look up this member in the source code.

      Toggle details void QMessageBox

      setDefaultButtonsetDefaultButton ( QPushButton *button ) ( QPushButton *button )

      Sets the message box's default button to button.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      setDefaultButtonsetDefaultButton ( StandardButton button ) ( StandardButton button )

      Sets the message box's default button to button.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      setEscapeButtonsetEscapeButton ( QAbstractButton *button ) ( QAbstractButton *button )

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

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      setEscapeButtonsetEscapeButton ( StandardButton button ) ( StandardButton button )

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

      See also addButton() and clickedButton().

      Look up this member in the source code.

      Toggle details void QMessageBox

      setVisiblesetVisible ( bool visible ) ( bool visible ) [virtual]

      Reimplemented from QWidget::setVisible().

      Look up this member in the source code.

      Toggle details void QMessageBox

      setWindowModalitysetWindowModality ( Qt::WindowModality windowModality ...) ( 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.

      Look up this member in the source code.

      Toggle details void QMessageBox

      setWindowTitlesetWindowTitle ( const QString &title ) ( const 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).

      Look up this member in the source code.

      Toggle details void QMessageBox

      showEventshowEvent ( QShowEvent *e ) ( QShowEvent *e ) [virtual protected]

      Reimplemented from QWidget::showEvent().

      Look up this member in the source code.

      Toggle details StandardButton QMessageBox

      standardButtonstandardButton ( QAbstractButton *button ...) ( QAbstractButton *button )const

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

      See also button() and standardButtons().

      Look up this member in the source code.

      Toggle details StandardButton QMessageBox

      warningwarning ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ...) ( QWidget *parent , const QString &title , const QString &text , StandardButtons buttons=Ok , StandardButton defaultButton=NoButton ) [static]

      Opens a warning message box with the given title and text in front of the specified parent widget.

      The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons. If defaultButton is QMessageBox::NoButton, QMessageBox chooses a suitable default automatically.

      Returns the identity of the standard button that was clicked. If Esc was pressed instead, the escape button is returned.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details QMessageBox

      QMessageBoxQMessageBox ( const QString &title , const QString &text , Icon icon , int button0 , int button1 , int button2 , QWidget *parent=0 , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ...) ( const QString &title , const QString &text , Icon icon , int button0 , int button1 , int button2 , QWidget *parent=0 , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ) Obsolete function

      Constructs a message box with a title, a text, an icon, and up to three buttons.

      The icon must be one of the following:

      Each button, button0, button1 and button2, can have one of the following values:

      Use QMessageBox::NoButton for the later parameters to have fewer than three buttons in your message box. If you don't specify any buttons at all, QMessageBox will provide an Ok button.

      One of the buttons can be OR-ed with the QMessageBox::Default flag to make it the default button (clicked when Enter is pressed).

      One of the buttons can be OR-ed with the QMessageBox::Escape flag to make it the cancel or close button (clicked when Esc is pressed).

      1.         QMessageBox mb("Application Name",
      2.                        "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
      3.                        QMessageBox::Question,
      4.                        QMessageBox::Yes | QMessageBox::Default,
      5.                        QMessageBox::No | QMessageBox::Escape,
      6.                        QMessageBox::NoButton);
      7.         if (mb.exec() == QMessageBox::No)  {
      8.             // try again

      The message box is an application modal dialog box.

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

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

      Look up this member in the source code.

      Toggle details QString QMessageBox

      buttonTextbuttonText ( int button ) ( int button )const Obsolete function

      Returns the text of the message box button button, or an empty string if the message box does not contain the button.

      Use button() and QPushButton::text() instead.

      See also setButtonText().

      Look up this member in the source code.

      Toggle details int QMessageBox

      criticalcritical ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1 , int button2=0 ...) ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1 , int button2=0 ) [static] Obsolete function

      Opens a critical message box with the given title and text. The dialog may have up to three buttons. Each of the button parameters, button0, button1 and button2 may be set to one of the following values:

      If you don't want all three buttons, set the last button, or last two buttons to QMessageBox::NoButton.

      One button can be OR-ed with QMessageBox::Default, and one button can be OR-ed with QMessageBox::Escape.

      Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.) of the button that was clicked.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      criticalcritical ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ...) ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ) [static] Obsolete function

      This is an overloaded function.

      Displays a critical error message box with the given title and text, as well as one, two, or three buttons. Returns the number of the button that was clicked (0, 1 or 2).

      button0Text is the text of the first button, and is optional. If button0Text is not supplied, "OK" (translated) will be used. button1Text is the text of the second button, and is optional, and button2Text is the text of the third button, and is optional. defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1, or 2 to make pressing Escape equivalent to clicking the relevant button.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      informationinformation ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1=0 , int button2=0 ...) ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1=0 , int button2=0 ) [static] Obsolete function

      Opens an information message box with the given title and the text. The dialog may have up to three buttons. Each of the buttons, button0, button1 and button2 may be set to one of the following values:

      If you don't want all three buttons, set the last button, or last two buttons to QMessageBox::NoButton.

      One button can be OR-ed with QMessageBox::Default, and one button can be OR-ed with QMessageBox::Escape.

      Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.) of the button that was clicked.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      informationinformation ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ...) ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ) [static] Obsolete function

      This is an overloaded function.

      Displays an information message box with the given title and text, as well as one, two or three buttons. Returns the index of the button that was clicked (0, 1 or 2).

      button0Text is the text of the first button, and is optional. If button0Text is not supplied, "OK" (translated) will be used. button1Text is the text of the second button, and is optional. button2Text is the text of the third button, and is optional. defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). escapeButtonNumber is the index of the escape button; pressing Esc is the same as clicking this button. It defaults to -1; supply 0, 1 or 2 to make pressing Esc equivalent to clicking the relevant button.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      questionquestion ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1=0 , int button2=0 ...) ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1=0 , int button2=0 ) [static] Obsolete function

      Opens a question message box with the given title and text. The dialog may have up to three buttons. Each of the buttons, button0, button1 and button2 may be set to one of the following values:

      If you don't want all three buttons, set the last button, or last two buttons to QMessageBox::NoButton.

      One button can be OR-ed with QMessageBox::Default, and one button can be OR-ed with QMessageBox::Escape.

      Returns the identity (QMessageBox::Yes, or QMessageBox::No, etc.) of the button that was clicked.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      questionquestion ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ...) ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ) [static] Obsolete function

      This is an overloaded function.

      Displays a question message box with the given title and text, as well as one, two or three buttons. Returns the index of the button that was clicked (0, 1 or 2).

      button0Text is the text of the first button, and is optional. If button0Text is not supplied, "OK" (translated) will be used. button1Text is the text of the second button, and is optional. button2Text is the text of the third button, and is optional. defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1 or 2 to make pressing Escape equivalent to clicking the relevant button.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details void QMessageBox

      setButtonTextsetButtonText ( int button , const QString &text ...) ( int button , const QString &text ) Obsolete function

      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.

      Use addButton() instead.

      See also buttonText().

      Look up this member in the source code.

      Toggle details QPixmap QMessageBox

      standardIconstandardIcon ( Icon icon ) ( Icon icon ) [static] Obsolete function

      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. QMessageBox::Question, QMessageBox::Information, QMessageBox::Warning or QMessageBox::Critical.

      Call QStyle::standardIcon() with QStyle::SP_MessageBoxInformation etc. instead.

      Look up this member in the source code.

      Toggle details int QMessageBox

      warningwarning ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1 , int button2=0 ...) ( QWidget *parent , const QString &title , const QString &text , int button0 , int button1 , int button2=0 ) [static] Obsolete function

      Opens a warning message box with the given title and text. The dialog may have up to three buttons. Each of the button parameters, button0, button1 and button2 may be set to one of the following values:

      If you don't want all three buttons, set the last button, or last two buttons to QMessageBox::NoButton.

      One button can be OR-ed with QMessageBox::Default, and one button can be OR-ed with QMessageBox::Escape.

      Returns the identity (QMessageBox::Ok or QMessageBox::No or ...) of the button that was clicked.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details int QMessageBox

      warningwarning ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ...) ( QWidget *parent , const QString &title , const QString &text , const QString &button0Text , const QString &button1Text=QString() , const QString &button2Text=QString() , int defaultButtonNumber=0 , int escapeButtonNumber=-1 ) [static] Obsolete function

      This is an overloaded function.

      Displays a warning message box with the given title and text, as well as one, two, or three buttons. Returns the number of the button that was clicked (0, 1, or 2).

      button0Text is the text of the first button, and is optional. If button0Text is not supplied, "OK" (translated) will be used. button1Text is the text of the second button, and is optional, and button2Text is the text of the third button, and is optional. defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1, or 2 to make pressing Escape equivalent to clicking the relevant button.

      The message box is an application modal dialog box.

      Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox constructors.

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

      Look up this member in the source code.

      Toggle details QMessageBox

      QMessageBoxQMessageBox ( const QString &title , const QString &text , Icon icon , int button0 , int button1 , int button2 , QWidget *parent , const char *name , bool modal , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ...) ( const QString &title , const QString &text , Icon icon , int button0 , int button1 , int button2 , QWidget *parent , const char *name , bool modal , Qt::WindowFlags f=Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ) Qt3-support

      Constructs a message box with the given parent, name, and window flags, f. The window title is specified by title, and the message box displays message text and an icon specified by text and icon.

      The buttons that the user can access to respond to the message are defined by button0, button1, and button2.

      Look up this member in the source code.

      Toggle details QMessageBox

      QMessageBoxQMessageBox ( QWidget *parent , const char *name ) ( QWidget *parent , const char *name ) Qt3-support

      Constructs a message box with the given parent and name.

      Look up this member in the source code.

      Toggle details int QMessageBox

      messagemessage ( const QString &title , const QString &text , const QString &buttonText=QString() , QWidget *parent=0 , const char *name=0 ...) ( const QString &title , const QString &text , const QString &buttonText=QString() , QWidget *parent=0 , const char *name=0 ) [static] Qt3-support

      Opens a modal message box with the given title and showing the given text. The message box has a single button which has the given buttonText (or tr("OK")). The message box is centred over its parent and is called name.

      Use information(), warning(), question(), or critical() instead.

      For example, if you have code like

      1. QMessageBox::message(tr("My App"), tr("All occurrences replaced."),
      2.                      tr("Close"), this);

      you can rewrite it as

      1.                     QMessageBox::information(this, tr("My App"),
      2.                          tr("All occurrences replaced."),
      3.                          QMessageBox::Close);

      Look up this member in the source code.

      Toggle details bool QMessageBox

      queryquery ( const QString &caption , const QString &text , const QString &yesButtonText=QString() , const QString &noButtonText=QString() , QWidget *parent=0 , const char *name=0 ...) ( const QString &caption , const QString &text , const QString &yesButtonText=QString() , const QString &noButtonText=QString() , QWidget *parent=0 , const char *name=0 ) [static] Qt3-support

      Queries the user using a modal message box with up to two buttons. The message box has the given caption (although some window managers don't show it), and shows the given text. The left button has the yesButtonText (or tr("OK")), and the right button has the noButtonText (or isn't shown). The message box is centred over its parent and is called name.

      Use information(), question(), warning(), or critical() instead.

      Look up this member in the source code.

      Toggle details QPixmap QMessageBox

      standardIconstandardIcon ( Icon icon , Qt::GUIStyle style ...) ( Icon icon , Qt::GUIStyle style ) [static] Qt3-support

      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. QMessageBox::Information, QMessageBox::Warning or QMessageBox::Critical.

      style is unused.

      Look up this member in the source code.

      Signals

      Toggle details void QMessageBox

      buttonClickedbuttonClicked ( QAbstractButton *button ) ( QAbstractButton *button ) [signal]

      This signal is emitted whenever a button is clicked inside the QMessageBox. The button that was clicked in returned in button.

      Look up this member in the source code.

      Public Slots

      Toggle details int QMessageBox

      execexec () () [slot]

      Shows the message box as a modal dialog, blocking until the user closes it.

      When using a QMessageBox with standard buttons, this functions returns a StandardButton value indicating the standard button that was clicked. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

      Users cannot interact with any other window in the same application until they close the dialog, either by clicking a button or by using a mechanism provided by the window system.

      See also show() and result().

      Look up this member in the source code.

      Notes provided by the Qt Community
      Informative
      • 4

      Votes: 2

      Coverage: Qt library 4.7, 4.8, 5.0

      Picture of daviddoria daviddoria

      Lab Rat
      6 notes

      Standard usage

      A standard usage is to display some specific buttons, and check the result. In this snippet we create a message box with two buttons, “yes” and “no”, and then output a message indicating which one was clicked.

      1.   QMessageBox msgBox;
      2.   msgBox.addButton(QMessageBox::Yes);
      3.   msgBox.addButton(QMessageBox::No);
      4.   msgBox.setText("My Message Box");
      5.  
      6.   int selection = msgBox.exec();
      7.   if(selection == QMessageBox::Yes)
      8.   {
      9.     qDebug() << "yes";
      10.   }
      11.   else if(selection == QMessageBox::No)
      12.   {
      13.     qDebug() << "no";
      14.   }

      [Revisions]