Qt的qApp->quit和QMessageBox一块踩过的坑

本文介绍在Qt项目中,如何在MainWindow构造函数中正确实现程序退出。通过使用QTimer结合qApp->quit()来确保程序能够及时响应退出指令,并提供了一个示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用MainWindow框架基础上做开发,在MainWindow的构造函数中,需要做各种资源、配置、外设等的的初始化,如果不成功,给出弹窗提示报错,应该直接退出程序。

按照网上搜索,Qt的程序在任意处想要退出使用了qApp->quit,发现该操作并不是真正的退出,后续程序继续执行,如果调用了MessageBox,会导致MessageBox闪退。

在窗体MainWindow构造函数中正确的退出程序方法:

        QTimer *myTimer = new QTimer();
        myTimer->start(100);
        connect(myTimer, &QTimer::timeout, this, [=](){
               //this->close();
               qApp->quit();
           });

即便如此,还需要检查任务管理器中(Windows平台),程序是否真正退出。

tryQtWidgetsApp::tryQtWidgetsApp(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); //这里也可以修改为自适应 ui.groupBox_8->move(10, 10); //从ini读取设置信息 QSettings settings("config.ini", QSettings::IniFormat); //读取串口号 QString serialName = settings.value("Option/serialNumber").toString(); int serialName_i = serialName.toInt(); //将spinbox显示为默认输入的值 ui.spinBox->setValue(serialName_i); //读取波特率 QString baudRate = settings.value("Option/baudRate").toString(); int baudRate_i = baudRate.toInt(); ui.comboBox->setCurrentText(baudRate); //将串口号应用到serialReader线程中 std::wstringstream ss; // 用流实现拼接 ss << TEXT("\\\\.\\COM") << settings.value("Option/serialNumber").toInt(); std::wstring portName = ss.str(); LPCWSTR COM_PORT = portName.c_str(); //const LPCWSTR COM_PORT = TEXT("\\\\.\\COM8"); const QString COM_PORT_QS = QString::fromStdWString(COM_PORT); //SerialReader* serialReader = new SerialReader("COM8", this); SerialReader* serialReader = new SerialReader(COM_PORT_QS, baudRate_i, this); DataMonitor* dataMonitor = new DataMonitor(serialReader); // 连接 SerialReader 的错误信号到主窗口的 showError 槽 connect(serialReader, &SerialReader::errorOccurred, this, &tryQtWidgetsApp::showError); // 启动串口读取线程 serialReader->start(); //链接 connect(dataMonitor, &DataMonitor::dataUpdated, this, &tryQtWidgetsApp::setLabel); /* connect(ui.pushButton_2, &QPushButton::clicked, this, [&]() { //初始化option窗口 Options* optionWindow = new Options(); optionWindow->setWindowModality(Qt::ApplicationModal); //获取主窗口信息 QRect mainGeometry = this->geometry(); //QSize windowSize = this->geometry().size(); // QRect的尺寸 //int width = windowSize.width(); //int height = windowSize.height(); // 设置窗口属性确保自动删除 optionWindow->setAttribute(Qt::WA_DeleteOnClose); optionWindow->setWindowTitle("设置"); optionWindow->show(); optionWindow->setGeometry(mainGeometry.adjusted(mainGeometry.height() / 2, mainGeometry.width() / 4, 0, 0)); }); */ QObject::connect(ui.pushButton_2, &QPushButton::clicked, this, [&]() { //ok按钮按下时候要写入一下设置的值 QSettings settings("config.ini", QSettings::IniFormat); //写入串口号 settings.setValue("Option/serialNumber", ui.spinBox->value()); //写入波特率 settings.setValue("Option/baudRate", ui.comboBox->currentText()); QMessageBox msgBox; msgBox.setIcon(QMessageBox::Question); msgBox.setWindowTitle("注意"); msgBox.setText("设置已保存,是否重启并应用?"); // 添加两个标准按钮 QPushButton* confirmBtn = msgBox.addButton(QMessageBox::Ok); // "确定"按钮 QPushButton* cancelBtn = msgBox.addButton(QMessageBox::Cancel); // "取消"按钮 msgBox.resize(400, 200); msgBox.setStyleSheet("QMessageBox { font-size: 16px; }"); // 执行对话框并获取点击结果 int ret = msgBox.exec(); //printf("1"); // 判断用户点击了哪个按钮 if (ret == QMessageBox::Ok) { qApp->quit(); QProcess::startDetached(qApp->applicationFilePath(), QStringList()); } else if (ret == QMessageBox::Cancel) { //this->close(); } }); }如何安全退出Qthread serialPort
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值