Qt 应用程序在退出时挂起(在 QDialog 执行之后)

发布于 2024-10-05 05:49:58 字数 767 浏览 4 评论 0原文

我正在使用 Qt 构建 UI,我需要在主应用程序窗口之前显示一个对话框窗口,以便用户可以选择一些文件来加载类似的内容。 我得到的是一个相当简单的主要内容:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IView *view = new QtView();
    view->showView();
    int rc = a.exec();
    std::cout << "exit" << std::endl;
    return rc;
}

QtView 类是 IView 接口的具体实现。 它也有 mainwindow 实例和 QDialog 实例。 int view->showView() 方法,这就是我所得到的:

void QtView::showView()
{
    this->_configDialog->exec();
    this->_mainAppWindow->show();
}

它工作正常,对话框打开,当用户单击“确定”时,exec 返回并出现主窗口。问题是,当我退出主窗口时,我得到一个僵尸进程,即使所有窗口都已关闭,应用程序似乎仍挂起,并且我从未在主窗口返回之前在主窗口中打印出“退出”。 我不确定我做错了什么,但即使我单击十字关闭对话框,主窗口也会打开,并且一旦关闭,所有内容都会挂在那里,我也会得到相同的结果。

如果有人有任何建议,那就太酷了。 谢谢。

I'm using Qt to build a UI and I need to have a dialog window show up before the main app window, so that the user can select some files to load ans things like that.
What I've got is a fairly simple main:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IView *view = new QtView();
    view->showView();
    int rc = a.exec();
    std::cout << "exit" << std::endl;
    return rc;
}

the QtView class is the concrete implementation of the IView interface.
It has the mainwindow instance and a QDialog instance too. int the view->showView() method this is what I've got:

void QtView::showView()
{
    this->_configDialog->exec();
    this->_mainAppWindow->show();
}

It works fine, the dialog opens and when the user clicks OK, exec returns and the main window appears. The problem is that when I quit the main window I get a zombie process and the app just seems to hang even though all the windows have been closes and I never get the "exit" I print out in the main just before the main returns.
I'm not sure what I'm doing wrong, but I get the same resutl even if I click on the cross to close the dialog, the main window opens up, and once closed the whole things just hangs there.

If anyone has any advice, that would be cool.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

太傻旳人生 2024-10-12 05:49:58

我解决了我的问题。
我使用来自 QDialog 的接受/拒绝/完成(int)信号来触发主应用程序窗口上的显示槽,并在对话框被接受时显示它。
不管怎样,谢谢你的帮助。

I Fixed my problem.
I used the accepted/rejected/finished(int) signal from the QDialog to trigger the show slot on the main app window, and display it if the dialog was accepted.
Thanks for the help anyway.

冷月断魂刀 2024-10-12 05:49:58

我认为这种行为的可能原因是您的配置对话框可能没有设置父小部件(这是一个盲目的猜测,因为您没有引用代码的相关部分)。这是因为默认情况下,QApplication 仅当所有没有父窗口的窗口都关闭时才会退出。问题是,从技术上讲,对话框不是窗口 - 它具有 Qt::Dialog 窗口类型,而不是 Qt::Window。这意味着任何关闭的“孤立”对话框仍将阻止应用程序自动退出。

解决方案是什么?
为对话框提供父级或启用 Qt::WA_QuitOnClose 属性:

this->_configDialog->setAttribute(Qt::WA_QuitOnClose);

I think the possible reason for this behavior is that your configuration dialog may not have a parent widget set on it (it's a blind guess though as you haven't cited the relevant portion of the code). That's because QApplication by default would only quit when all the windows with no parent are closed. And the thing is, a dialog is not a window, technically - it has the Qt::Dialog window type rather than Qt::Window. That means any "orphaned" dialog, that is closed, will still prevent the application from exiting automatically.

The solution?
Either giving a parent to the dialog or enabling the Qt::WA_QuitOnClose attribute:

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