Qt-关闭QMessageBox之后程序自动关闭了
source link: https://discretetom.github.io/posts/qt-app-quit-after-closing-qmexxage-box/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Qt-关闭QMessageBox之后程序自动关闭了
Qt-关闭QMessageBox之后程序自动关闭了
以MainWindow为parent的QMessageBox在运行结束时如果MainWindow处于hide的状态会调用MainWindow的析构函数把MainWindow杀了
看下面这人畜无害的代码
MainWindow::~MainWindow()
{
qDebug() << "MainWindow destroyed.";
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox * box = new QMessageBox(this);
hide();
box->exec();
// box->show();
}
以MainWindow为parent新建一个QMessageBox
隐藏MainWindow并显示/执行QMessageBox
在点击MessageBox上面的OK按钮 MessageBox消失之后
MainWindow的析构函数会被调用
这样做的原因应该是防止MainWindow在hide状态下无法简单关闭的问题
所以在最后一个可见的窗体消失时杀掉父窗体MainWindow
嗯 这么一说还是可以理解的
但是我如果把程序缩小到托盘 弹一个MessageBox程序就没了。。。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK