|  | Home · Overviews · Examples |

# QMessageBox Class Reference
[com.trolltech.qt.gui 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`.

## 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:

| Question | For message boxes that ask a question as part of normal operation. Some style guides recommend using Information for this purpose.
| Information | For message boxes that are part of normal operation.
| Warning | For message boxes that tell the user about unusual errors.
| Critical | For 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 `ButtonRole`s 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. 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 `QDialogButtonBox`, GUI Design Handbook: Message Box, Standard Dialogs Example, and Application Example.