【Qt】窗口关闭提示框

在关闭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;
}
相关推荐
博客18001 小时前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴3 小时前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
渣波3 小时前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
众少成多积小致巨20 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
倔强的石头_1 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
xcyxiner2 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner3 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner3 天前
DicomViewer (添加模型类)3
qt
xcyxiner4 天前
DicomViewer (目录调整) 2
qt
xcyxiner4 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt