开发者

Problem with hidden QMainWindow: application crashes after QMessageBox is displayed

// 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());
}

voi开发者_JAVA技巧d 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.


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:

int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  MainWindow* window = new MainWindow();
  window->show();
  int ret = app.exec();
  delete window;
  return ret;
}

Alternatively you can set the application as the window's parent


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:

...
app.setQuitOnLastWindowClosed(false);
...

to your:

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
...
    app.setQuitOnLastWindowClosed(false);
...
    MainWindow* window = new MainWindow();
    window->show();
    return app.exec();
}

this should help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜