主窗口关闭时 QWidget 不会关闭 [英] QWidget doesn't close when main window is closed

查看:232
本文介绍了主窗口关闭时 QWidget 不会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个主窗口 (QWidget),当单击按钮时它会打开一个新的 QWidget,但是当我关闭主窗口时,最近打开的 QWidget 没有关闭.

I'm trying to make a main window (QWidget) which open a new QWidget when a button is clicked but when I close the main window, the QWidget recently opened doesn't close.

ma​​in.cpp

QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();

ma​​inwindow.cpp(父)

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

out.cpp(子)

Out::Out(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Out)
{
    ui->setupUi(this);

}

推荐答案

我怀疑你正在寻找 Qt::WA_QuitOnClose:

当最后一个带有属性的小部件时,使 Qt 退出应用程序set 已接受 closeEvent().可以使用以下命令修改此行为QApplication::quitOnLastWindowClosed 属性.默认这个为 Qt::Window 类型的所有小部件设置了属性.

Makes Qt quit the application when the last widget with the attribute set has accepted closeEvent(). This behavior can be modified with the QApplication::quitOnLastWindowClosed property. By default this attribute is set for all widgets of type Qt::Window.

在这种情况下,您可能应该调用:

In this case, you should probably call:

myWidget->setAttribute( Qt::WA_QuitOnClose, false );

这篇关于主窗口关闭时 QWidget 不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆