diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index df17399ca7e..f9d58e4d906 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1657,12 +1657,15 @@ void QMessageBox::open(QObject *receiver, const char *member) void QMessageBoxPrivate::setVisible(bool visible) { - Q_Q(QMessageBox); - // Last minute setup - if (autoAddOkButton) - q->addButton(QMessageBox::Ok); - detectEscapeButton(); + if (visible) { + Q_Q(QMessageBox); + if (autoAddOkButton) + q->addButton(QMessageBox::Ok); + detectEscapeButton(); + } + // On hide, we may be called from ~QDialog(), so prevent accessing + // q_ptr as a QMessageBox! if (canBeNativeDialog()) setNativeDialogVisible(visible); @@ -1670,7 +1673,7 @@ void QMessageBoxPrivate::setVisible(bool visible) // Update WA_DontShowOnScreen based on whether the native dialog was shown, // so that QDialog::setVisible(visible) below updates the QWidget state correctly, // but skips showing the non-native version. - q->setAttribute(Qt::WA_DontShowOnScreen, nativeDialogInUse); + static_cast(q_ptr)->setAttribute(Qt::WA_DontShowOnScreen, nativeDialogInUse); QDialogPrivate::setVisible(visible); }