MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QMenuBar *bar = menuBar(); QMenu *pFile = bar->addMenu("文件"); QAction *pNew = pFile->addAction("新建"); QAction *oPen = pFile->addAction("打开"); connect(pNew, &QAction::triggered, [=]() { // dig.show(); // qDebug() << "1111"; // QDialog *p = new QDialog(this); // p->show(); QDialog *p = new QDialog(this); p->setAttribute(Qt::WA_DeleteOnClose); p->show(); } ); }
setAttribute(Qt::WA_DeleteOnClose);让子窗口关闭时自动析构,不用等父对象析构时析构,及时回收内存。