information
QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
这是比较常用的一种用法,效果如下:
information原型:
StandardButton QMessageBox::information(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton) [static]
- 第一个参数是父控件指针
- 第二个参数是标题
- 第三个参数是内容
- 第四个参数是窗口里面要多少个按钮(默认为OK)
- 第五个参数指定按下Enter时使用的按钮。(默认为NoButton,此时QMessageBox会自动选择合适的默认值。)
示例1:
QMessageBox::information(NULL, "Title", "Content");
此时第四第五为默认参数,效果:
示例2:
QMessageBox::information(NULL, "Title", "Content",QMessageBox::Yes|QMessageBox::No);
此时效果(与图1相同):
示例三:
QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes|QMessageBox::No| QMessageBox::Abort);
添加多个按钮用|运算符连接,效果:
按钮类型参考:
enum StandardButton {
// keep this in sync with QDialogButtonBox::StandardButton
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, FirstButton = Ok, // internal
LastButton = RestoreDefaults, // internal YesAll = YesToAll, // obsolete
NoAll = NoToAll, // obsolete Default = 0x00000100, // obsolete
Escape = 0x00000200, // obsolete
FlagMask = 0x00000300, // obsolete
ButtonMask = ~FlagMask // obsolete
};
会创建消息提示框后,我们怎么知道用户点了什么呢,看如下小例子:
QMessageBox:: StandardButton result= QMessageBox::information(NULL, "Title", "Content",QMessageBox::Yes|QMessageBox::No);
switch (result)
{
case QMessageBox::Yes:
qDebug()<<"Yes";
break;
case QMessageBox::No:
qDebug()<<"NO";
break;
default:
break;
}
critical
critical adj. 关键的; 批评的,爱挑剔的; 严重的; 极重要的;
QMessageBox::critical(NULL, "critical", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
效果:
warning
QMessageBox::warning(NULL, "warning", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
效果:
question
QMessageBox::question(NULL, "question", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
效果:
about
原型:static void about(QWidget *parent, const QString &title, const QString &text);
QMessageBox::about(NULL, "About", "by hjwblog.com");
效果: