1, this specifies the main window as the parent class
void MainWindow::on_pushButton_clicked()
{
Dialog *aa = new Dialog(this);
aa->show();
}
2. Rewrite the main window closeEvent event
mainwindow.h
QVector<Dialog*> m_test;
mainwindow.cpp
QVector<Dialog*> m_test;
void MainWindow::closeEvent(QCloseEvent *event)
{
qDebug() << "close";
foreach(Dialog *cc, m_test)
{
if(cc != nullptr)
{
delete cc;
cc = nullptr;
}
}
}
void MainWindow::on_pushButton_clicked()
{
Dialog *aa = new Dialog();
m_test.append(aa);
aa->show();
}
When the child window is closed, the child window object is deleted at the same time, and the resource can be released using the Qt::WA_DeleteOnClose property
void MainWindow::on_pushButton_clicked()
{
Dialog *aa = new Dialog();
aa->setAttribute(Qt::WA_DeleteOnClose, true);
aa->show();
}