![]() |
Home · Overviews · Examples |
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.
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.
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.
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. |
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.
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:
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.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.5_01 |