隐藏 QMainWindow 的问题:显示 QMessageBox 后应用程序崩溃

发布于 2024-10-19 08:03:23 字数 1642 浏览 9 评论 0原文

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-10-26 08:03:23

问题似乎如下:当对话框关闭时,应用程序认为没有更多窗口打开(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;
}

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

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

东风软 2024-10-26 08:03:23

我不确定,但我认为当 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.

猫烠⑼条掵仅有一顆心 2024-10-26 08:03:23

只需尝试以下操作 - 将这个:

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

这应该会有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文