int
i
=
QMessageBox
::
warning
(
this
,
"QMessageBox"
,
"The
document
contains
unsaved
changes\n"
"Do you want to save the changes before exiting?",
tr("确定"),tr("不确定"),tr("取消"));
运行后,按相应的按钮,i的返回值分别为0,1,2;然后你可根据相应的返回值,进行自己的操作;
当然,别忘了在main()中加上对中文的支持:
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
注:Qt提供了五个类似的static接口,我们使用类名就可以访问,如:QMessageBox::information()、QMessageBox::critical()、QMessageBox::about()等,用于显示类似的窗口,但是最多只支持三个按钮;
若想支持多个按钮,就不能使用这几个static的函数,需要自定义QMessagebox来使用:
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(tr("警告!"));
msgBox.setText(tr("警告,请正确选择!"));
QPushButton *Button1 = msgBox.addButton(tr("确定"),QMessageBox::AcceptRole);
QPushButton *Button2 = msgBox.addButton(tr("取消"),QMessageBox::RejectRole);
QPushButton *Button3 = msgBox.addButton(tr("是"),QMessageBox::YesRole);
QPushButton *Button4 = msgBox.addButton(tr("否"),QMessageBox::NoRole);
msgBox.exec();
然后
if (msgBox.clickedButton() == Button1)
{
//do...
}
else if (msgBox.clickedButton() == Button2)
{
//do...
}
.........