【问题标题】:Problem with hidden QMainWindow: application crashes after QMessageBox is displayed隐藏 QMainWindow 的问题:显示 QMessageBox 后应用程序崩溃
【发布时间】:2011-07-04 05:45:49
【问题描述】:
// 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):

此属性保存当最后一个窗口关闭时应用程序是否隐式退出。 默认值为真。如果此属性为真,则当最后一个 可见窗口(即没有父窗口的窗口)具有 Qt::WA_QuitOnClose 属性时,应用程序退出集已关闭。默认情况下,为除子窗口之外的所有小部件设置此属性。关于 Qt::Window 对象的详细列表,请参阅 Qt::WindowType。

QMainWindow 隐藏后,没有可见窗口。当 QMessageBox 关闭时,应用程序退出

【问题讨论】:

    标签: c++ qt qt4 qmainwindow qmessagebox


    【解决方案1】:

    我不确定,但我认为当 QMessageBox 关闭时,它正试图将焦点返回到他的父级(您的 MainWindow)女巫被隐藏。此操作失败,系统正在抛出异常。

    【讨论】:

    • 试试这个函数:void MainWindow::showMessageBox() { QMessageBox::information(this, "Hello,", "world!", QMessageBox::Ok); this-&gt;show(); } with out set a.setQuitOnLastWindowClosed(false); 如果它不会崩溃 - 这意味着它无法将焦点返回到 MainWindow 并抛出异常。
    • 传递给QMessageBox::information(...) NULL 而不是this。 MessageBox 现在没有父级,但应用程序终止。
    • 我重新实现了focusInEvent - QMessageBox 不要尝试返回焦点。
    【解决方案2】:

    问题似乎如下:当对话框关闭时,应用程序认为没有更多的窗口打开(setQuitOnLastWindowClosed指的是可见的顶级窗口),所以它退出了。不会调用窗口的析构函数,因为您从不删除对象!

    这应该打印出消息:

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

    或者,您可以将应用程序设置为窗口的父级

    【讨论】:

    • 不幸的是,您不能将应用程序设置为 MainWindow 父级,因为父子关系要求父级是 QWidget 的后代; QApplication 来自 QObject 而不是 QWidget。
    【解决方案3】:

    试试下面的 - 把这个:

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

    给你的:

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

    这应该有帮助!

    【讨论】:

    • 太好了!解决了我的问题,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多