隐藏QMainWindow的问题:显示QMessageBox后应用程序崩溃
// main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow* window = new MainWindow();
window->show();
return app.exec();
}
// mainwindow.cpp
#include <QTimer>
#include <QMessageBox>
#include <iostream>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
this->setCentralWidget(new QWidget());
}
void MainWindow::mousePressEvent(QMouseEvent* event)
{
this->hide();
QTimer* timer = new QTimer();
timer->setInterval(3*1000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(showMessageBox()));
}
void MainWindow::showMessageBox()
{
QMessageBox::information(this, "Hello,", "world!", QMessageBox::Ok);
}
MainWindow::~MainWindow()
{
std::cerr << "Destructor called" << std::endl;
}
我点击窗口 - 它隐藏,QMessageBox出现。我单击“确定” - 应用程序终止,并且不调用MainWindow的析构函数。为什么申请终止?也许我错过了什么? Qt 4.7.0,Linux。
......哎呀!看起来我找到了我需要的东西。
a.setQuitOnLastWindowClosed(false);
当我需要它时,我使用a.exit(0)终止app。但我还是不明白出了什么问题。
是啊!看起来我明白出了什么问题。这是关于方法的信息
QApplication::quitOnLastWindowClosed(bool)
:
此属性保存应用程序在最后一个窗口关闭时是否隐式退出。
默认值为true。如果此属性为true,则应用程序在关闭具有Qt :: WA_QuitOnClose属性集的最后一个可见主窗口(即没有父窗口的窗口)时退出。默认情况下,为子窗口以外的所有窗口小部件设置此属性。有关Qt :: Window对象的详细列表,请参阅Qt :: WindowType。
隐藏QMainWindow后,没有可见的窗口。当QMessageBox关闭时,应用程序退出。
没有找到相关结果
已邀请:
2 个回复
厢界山攀
指的是可见的顶级窗口),因此它退出。不会调用窗口的析构函数,因为您永远不会删除该对象! 这应该打印出消息:
或者,您可以将应用程序设置为窗口的父级
摊揉售