在关闭QWdiget窗口时弹出提示框
重写**closeEvent**函数
cpp
void closeEvent(QCloseEvent* event) override;
cpp
QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, "信息提示", "是否保存当前数据?", QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel, this, Qt::WindowStaysOnTopHint);
msgBox->setDefaultButton(QMessageBox::No);
msgBox->button(QMessageBox::Save)->setText(u8"保存");
msgBox->button(QMessageBox::No)->setText(u8"不保存");
msgBox->button(QMessageBox::Cancel)->setText(u8"取消");
int res = msgBox->exec();
if (res == QMessageBox::Save)
{
model->saveDate();
}
else if (res == QMessageBox::Cancel)
{
event->ignore();
}
delete msgBox;
this->close();
通过按钮在点击时弹窗
在按钮对应的槽函数中,适用于无边框的窗口
cpp
// 退出系统
void ExitAction(bool state)
{
if_Data = 0;
QMessageBox* msgBox = new QMessageBox(QMessageBox::Question, "提示", "退出系统?", QMessageBox::Save /*| QMessageBox::No */ | QMessageBox::Cancel, this, Qt::WindowStaysOnTopHint);
msgBox->setDefaultButton(QMessageBox::No);
msgBox->button(QMessageBox::Save)->setText(u8"确定");
msgBox->button(QMessageBox::Cancel)->setText(u8"取消");
int res = msgBox->exec();
if (res == QMessageBox::Save)
{
qApp->quit();
}
delete msgBox;
}