QWidget doesn't close when main window is closed

青春壹個敷衍的年華 提交于 2019-12-23 08:28:32

问题


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.

main.cpp

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

mainwindow.cpp (parent)

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

out.cpp (child)

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

}

回答1:


I suspect you're looking for Qt::WA_QuitOnClose:

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 );


来源:https://stackoverflow.com/questions/16468584/qwidget-doesnt-close-when-main-window-is-closed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!