Reputation: 31
I have a Mainwindow that creates a QDialog(that its name is qds) when I press a button. When I close the Mainwindow, I want QDialog is closed too. But, when I close the Mainwindow, QDialog is still open and application is still running. This is the mainwindow's destructor:
MainWindow::~MainWindow(){
if(qds) delete qds; // this is the QDialog
// ...other code
}
qds is not mainwindow's child. I tried with to put
setAttribute(Qt::WA_DeleteOnClose);
in mainwindow's constructor, but it generates segmentation faul(double free).
Upvotes: 0
Views: 1456
Reputation: 8698
The non-modal dialog is launched and the pointer is in main window object. It prevents the app from quitting while closing the main window. How to fix that?
The application event loop should not have more objects 'spinning' in it and that solves the problem. I call all the widgets that don't have the other widget 'this' pointer passed via constructor 'detached'. But we can still track them. I use lists of 'detached' widgets but with just one 'detached' dialog the class member variable pointer is enough.
void MainWindow::closeEvent(QCloseEvent *event)
{
// TODO: also make sure m_pDetachedNonmodalDlg set to null
// when the dialog closed on its own and deleted: see
// QObject::destroyed() signal for that or make it like
// QPointer<QWidget> m_pDetachedWidget
if (m_pDetachedNonmodalDlg)
m_pDetachedNonmodalDlg->close();
// or event->accept(); but fine 'moments' are there
QMainWindow::closeEvent(event);
}
Upvotes: 4