使用方式为
/***消息框的使用 *********************/
//提示框
//QMessageBox::about(this,"about me","helloworld");
// QMessageBox::aboutQt(this,"about me"); //输出关于Qt的详细信息
//警告窗口
//int bs= QMessageBox::critical(this,"about me","show info",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));
//输出信息提示框
//int bs= QMessageBox::information(this,"about me","information",QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::Discard|QMessageBox::Save,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));
其他使用方式:
enum QMessageBox::StandardButton
/***消息框的使用 *********************/
//提示框
//QMessageBox::about(this,"about me","helloworld");
// QMessageBox::aboutQt(this,"about me"); //输出关于Qt的详细信息
//警告窗口
//int bs= QMessageBox::critical(this,"about me","show info",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));
//输出信息提示框
//int bs= QMessageBox::information(this,"about me","information",QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::Discard|QMessageBox::Save,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));
其他使用方式:
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();
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;
}
| Question | For asking a question during normal operations. |
| Information | For reporting information about normal operations. |
| Warning | For reporting non-critical errors. |
| Critical | For reporting critical errors. |
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);
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
}
Constant | Value | Description |
---|---|---|
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.
enum QMessageBox::Icon
This enum has the following values:
Constant | Value | Description |
---|---|---|
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. |
enum QMessageBox::StandardButton
flags QMessageBox::StandardButtons
These enums describe flags for standard buttons. Each button has a defined ButtonRole.
Constant | Value | Description |
---|---|---|
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 theDestructiveRole. |
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:
Constant | Value | Description |
---|---|---|
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 |