Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions

QMessageBox Class Reference
[QtGui module]

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

 #include <QMessageBox>

Inherits QDialog.

Public Types

Properties

Public Functions

Public Slots

Static Public Members

Additional Inherited Members


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.

 QMessageBox msgBox;
 msgBox.setText("The document has been modified.");
 msgBox.exec();

The user must click the OK button to dismiss the message box. The rest of the GUI is blocked until the message box is dismissed.

A better approach than just alerting the user to an event is to also ask the user what to do about it. Store the question in the informative 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.

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

This is the approach recommended in the 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.

 switch (ret) {
   case QMessageBox::Save:
       // Save was clicked
       break;
   case QMessageBox::Discard:
       // Don't Save was clicked
       break;
   case QMessageBox::Cancel:
       // Cancel was clicked
       break;
   default:
       // should never be reached
       break;
 }

To give the user more information to help him answer the question, set the detailed 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.

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.

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

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,

 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
 }

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.


Member Type Documentation

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

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

enum QMessageBox::StandardButton
flags QMessageBox::StandardButtons

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.


Property Documentation

detailedText : 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.

This property was introduced in Qt 4.2.

Access functions:

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

icon : 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.

Access functions:

See also iconPixmap.

iconPixmap : 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.

Access functions:

See also icon.

informativeText : 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.

This property was introduced in Qt 4.2.

Access functions:

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

standardButtons : 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.

This property was introduced in Qt 4.2.

Access functions:

See also addButton().

text : 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.

Access functions:

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

textFormat : 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.

Access functions:

See also setText().


Member Function Documentation

QMessageBox::QMessageBox ( QWidget * parent = 0 )

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

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

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.

QMessageBox::QMessageBox ( 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.

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

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

QMessageBox::~QMessageBox ()

Destroys the message box.

void QMessageBox::about ( 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".

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

void QMessageBox::aboutQt ( 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.

See also QApplication::aboutQt().

void QMessageBox::addButton ( QAbstractButton * button, ButtonRole role )

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 ( const 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 ( 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 ( 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.

This function was introduced in Qt 4.2.

See also standardButtons and standardButton().

QAbstractButton * QMessageBox::clickedButton () 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:

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

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

Opens a critical message box with the specified title and text. The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. 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.

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

This function was introduced in Qt 4.2.

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

QPushButton * QMessageBox::defaultButton () const

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

QAbstractButton * QMessageBox::escapeButton () 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.

This function was introduced in Qt 4.2.

See also setEscapeButton() and addButton().

int QMessageBox::exec ()   [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().

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

Opens an information message box with the specified title and text. The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. 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.

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

This function was introduced in Qt 4.2.

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

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

Opens a question message box with the specified title and text. The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. 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.

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

This function was introduced in Qt 4.2.

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

void QMessageBox::removeButton ( QAbstractButton * button )

Removes button from the button box without deleting it.

This function was introduced in Qt 4.2.

See also addButton() and setStandardButtons().

void QMessageBox::setDefaultButton ( 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().

void QMessageBox::setDefaultButton ( 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().

void QMessageBox::setEscapeButton ( 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().

void QMessageBox::setEscapeButton ( 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().

void QMessageBox::setWindowModality ( 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.

void QMessageBox::setWindowTitle ( 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).

This function was introduced in Qt 4.2.

StandardButton QMessageBox::standardButton ( 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.

This function was introduced in Qt 4.2.

See also button() and standardButtons().

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

Opens a warning message box with the specified title and text. The standard buttons are added to the message box. defaultButton specifies the button used when Enter is pressed. 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.

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

This function was introduced in Qt 4.2.

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


Copyright © 2008 Nokia Trademarks
Qt 4.4.3