0

I am opening a QDialog/QMessageBox using exec() to get user input. But before the user selects Save or Discard from the message box, the parent window from which it was opened is closed.

When this happens the application crashed.

Can the QDialog/QMessageBox be closed when the parent is closed. ?

QMessageBox msgBox;
    msgBox.setParent(parent);
    msgBox.setWindowTitle("Save Changes");
    msgBox.setWindowModality(Qt::ApplicationModal);
    msgBox.setText(QString("XXXX have been added to %1.").arg(YYY));
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
    msgBox.setDefaultButton(QMessageBox::Save);
    msgBox.setIcon(QMessageBox::Question);
    int userSelection = msgBox.exec();

    switch (userSelection) {
    case QMessageBox::Save:
    {
        onSubmitClicked();
        reload();
    }
    break;
    case QMessageBox::Discard:
    {
        sList.clear();
        reload();
    }
    break;
    default:
        break;
    }

Window closing code :

if (window != nullptr && window->isVisible())
        {
            window->close();
        }
ndnine89
  • 137
  • 2
  • 3
  • 12
  • Use signal aboutToQuit() and handle closing in SLOT – Amol Saindane Sep 28 '17 at 11:21
  • There are other windows open in the same application. Isn't aboutToQuit() emitted when the application itself is about to quit ? – ndnine89 Sep 28 '17 at 11:30
  • How can you close the parent window if message box as a modal window and blocks? – vahancho Sep 28 '17 at 11:46
  • @vahancho by calling close() from the mainwindow. Im closing a sub window. – ndnine89 Sep 28 '17 at 11:50
  • The messagebox is modal, how does the mainwindow call close? can't be by user input. You could override the closeEvent function and emit a signal liake aboutToClose. Then when constructing the message box, connect this signal to the messagebox close slot – choosyg Sep 28 '17 at 12:21
  • @choosyg Receives a signal from backend to close down the window. Tried your method, but the QEventLoop started by the exec() still causes a crash. – ndnine89 Sep 28 '17 at 12:47
  • Try to connect to deleteLater() instead and / or Inside the overridden closeEvent call QApplication::processEvents(ExcludeUserInput) to make the signal be processed. Then the MsgBox should be dead by the end of the close event handler. This has to be done before referring to QWidget::closeEvent(event) – choosyg Sep 29 '17 at 05:21

0 Answers0