QWidget does not close when closing the main window

I am trying to create a main window (QWidget) that opens a new QWidget when a button is clicked, but when I close the main window, a recently opened QWidget does not 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); } 
+4
source share
1 answer

I suspect you are looking for Qt::WA_QuitOnClose :

Causes Qt to exit the application when the last widget with the set attribute took closeEvent (). This behavior can be changed using the QApplication :: quitOnLastWindowClosed property. By default, this attribute is set for all widgets like Qt :: Window.

In this case, you should probably call:

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

Source: https://habr.com/ru/post/1479975/


All Articles