QMessageBox类提供了常用的弹出式对话框:提示、警告、错误、询问、关于对话框
原型如下:
void about ( QWidget * parent, const QString & title, const QString & text ) void aboutQt ( QWidget * parent, const QString & title = QString() ) StandardButton critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) StandardButton information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) StandardButton question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) StandardButton warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
(1)title表示标题
(2)text是对话框中的内容信息
(3)buttons是要显示的按钮,它可以是:
NoButton = 0x00000000, Ok = 0x00000400, Save = 0x00000800, SaveAll = 0x00001000, Open = 0x00002000, Yes = 0x00004000, YesToAll = 0x00008000, No = 0x00010000, NoToAll = 0x00020000, Abort = 0x00040000, Retry = 0x00080000, Ignore = 0x00100000, Close = 0x00200000, Cancel = 0x00400000, Discard = 0x00800000, Help = 0x01000000, Apply = 0x02000000, Reset = 0x04000000, RestoreDefaults = 0x08000000,
(4)defaultButton是默认激活的按钮
(5)对话框的返回值即是用户最终点击的按钮。
示例:
void Dialog::criticalMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::critical(this, tr("QMessageBox::critical()"), MESSAGE, QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore); if (reply == QMessageBox::Abort) criticalLabel->setText(tr("Abort")); else if (reply == QMessageBox::Retry) criticalLabel->setText(tr("Retry")); else criticalLabel->setText(tr("Ignore")); } void Dialog::informationMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE); if (reply == QMessageBox::Ok) informationLabel->setText(tr("OK")); else informationLabel->setText(tr("Escape")); } void Dialog::questionMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, tr("QMessageBox::question()"), MESSAGE, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); if (reply == QMessageBox::Yes) questionLabel->setText(tr("Yes")); else if (reply == QMessageBox::No) questionLabel->setText(tr("No")); else questionLabel->setText(tr("Cancel")); } void Dialog::warningMessage() { QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"), MESSAGE, 0, this); msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole); msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole); if (msgBox.exec() == QMessageBox::AcceptRole) warningLabel->setText(tr("Save Again")); else warningLabel->setText(tr("Continue")); }
界面: