隐藏 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) 终止应用程序。但我还是不明白出了什么问题。
是啊!看来我明白出了什么问题。 这是有关方法
QApplication::quitOnLastWindowClosed(bool)
的信息:
此属性保存最后一个窗口关闭时应用程序是否隐式退出。 默认为 true。如果此属性为 true,则当最后一个具有 Qt::WA_QuitOnClose 属性的可见主窗口(即没有父窗口的窗口)时,应用程序退出设置已关闭。默认情况下,为除子窗口之外的所有小部件设置此属性。有关 Qt::Window 对象的详细列表,请参阅 Qt::WindowType。
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;
}
I click the window - it hides and QMessageBox appears. I click "OK" - application terminates, and MainWindow's destructor isn't called. Why does application terminate? Maybe I've missed something? Qt 4.7.0, Linux.
... Oops! It looks like I found what I need.
a.setQuitOnLastWindowClosed(false);
When I need it, I terminate app using a.exit(0). But I still don't understand what was wrong.
Yeah! Looks like I understand what was wrong. This is information about method
QApplication::quitOnLastWindowClosed(bool)
:
This property holds whether the application implicitly quits when the last window is closed.
The default is true. If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except for sub-windows. Refer to Qt::WindowType for a detailed list of Qt::Window objects.
After QMainWindow is hidden, there is no visible windows. When QMessageBox is closed, application quits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题似乎如下:当对话框关闭时,应用程序认为没有更多窗口打开(
setQuitOnLastWindowClosed
指可见的顶级窗口),因此它退出。窗口的析构函数不会被调用,因为您从未删除该对象!这应该打印出消息:
或者,您可以将应用程序设置为窗口的父窗口
The issue seems to be the following: When the dialog box is closed, the application thinks that there are no more windows open (
setQuitOnLastWindowClosed
refers to visible top-level windows), so it quits. The destructor of your window is not called because you never delete the object!This should print out the message:
Alternatively you can set the application as the window's parent
我不确定,但我认为当 QMessageBox 关闭时,它试图将焦点返回到隐藏的父级(您的主窗口)女巫。此操作失败,系统抛出异常。
I'm not sure, but I think when QMessageBox has closed it is trying to return focus to his parent (Your MainWindow) witch is hidden. This operation fails, and system is throwing an exception.
只需尝试以下操作 - 将这个:
发送给您:
这应该会有所帮助!
just try the following - put this:
to your:
this should help!