来源:微信公众号「编程学习基地」
文章目录
1、消息对话框
//简单对话框
QMessageBox msgBox;
msgBox.setText("消息内容");
msgBox.exec();
//消息对话框
QMessageBox msgBox;
msgBox.setText("消息标题");
msgBox.setInformativeText("消息内容");
//加入三个按钮
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;
}
对话框类型:
对话框类型:
QMessageBox::NoIcon 0
QMessageBox::Question 4
QMessageBox::Information 1
QMessageBox::Warning 2
QMessageBox::Critical 3
QMessageBox::StandardButton reply;
reply = QMessageBox::critical(this, "标准警告", "警告", // critical警告对话框
QMessageBox::Abort | QMessageBox::Retry);
QMessageBox::StandardButton reply;
中reply是接受消息的对象可以与下面的相匹配.
QMessageBox::NoButton
QMessageBox::Ok
QMessageBox::Cancel
QMessageBox::Yes
QMessageBox::No
QMessageBox::Abort
QMessageBox::Retry
QMessageBox::Ignore
QMessageBox::YesAll
QMessageBox::NoAll
2、自定义标准对话框
QMessageBox msgBox;
msgBox.setText("内容标题"); //设置窗口文字提示
msgBox.setWindowTitle("标题"); //设置窗口标题
msgBox.setInformativeText("内容?"); //设置窗口消息
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.exec();//显示窗口
//定义对话框响应事件
if(msgBox.clickedButton()==Save){
//消息处理
}
QMessageBox box(QMessageBox::NoIcon, "提示", "是否删除该通道");
box.setWindowIcon(QIcon(":/res/favicon.ico"));
QPushButton *yesbutton = box.addButton("是",QMessageBox::YesRole);
QPushButton *nobutton = box.addButton("否",QMessageBox::NoRole);
//这个按键的作用, 是添加右上角的小红叉关闭键
QPushButton *closebutton = box.addButton("关闭",QMessageBox::RejectRole);
closebutton->hide();
box.exec();
//定义对话框响应事件
if(box.clickedButton()==nobutton){
//消息处理
return;
}