QMessageBox: Do last minute setup in setVisible instead of showEvent

The native message dialogs are shown in QMessageBox::setVisible,
instead of plumbed via e.g. QWidgetPrivate::show_sys (which is not
virtual). And we try to show the native dialog before we call the
base class QDialog::setVisible(), where we end up sending the show
event.

As the native dialogs might rely on the last minute setup of adding
an OK button or detecting the escape button, we need to do it in
setVisible instead.

We can not switch the order of showing the native dialogs after
calling the base class setVisible (and getting showEvent), as
we need to know whether the native dialog could be shown or not
to decide whether to show the fallback widget dialog.

The part from showEvent that added the detailsButton has been
removed, as we're already doing that in setDetailsText().

Pick-to: 6.5
Change-Id: I23f1d87d542d9dadfd71924f8119c22720f4b276
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
(cherry picked from commit b0ddca18d73430771f52c034e3d5a1bb92363f64)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-10-22 16:22:27 +02:00 committed by Qt Cherry-pick Bot
parent ed1def47a2
commit fa74a99c8e
2 changed files with 5 additions and 16 deletions

View File

@ -182,16 +182,6 @@ bool QCocoaMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality w
for (auto customButton : customButtons)
addButton(customButton.label, customButton.id, customButton.role);
// QMessageDialog's logic for adding a fallback OK button if no other buttons
// are added depends on QMessageBox::showEvent(), which is too late when
// native dialogs are in use. To ensure there's always an OK button with a tag
// we recognize we add it explicitly here as a fallback.
if (!m_alert.buttons.count) {
addButton(platformTheme->standardButtonText(StandardButton::Ok),
StandardButton::Ok, ButtonRole::AcceptRole);
}
if (auto checkBoxLabel = options()->checkBoxLabel(); !checkBoxLabel.isNull()) {
checkBoxLabel = QPlatformTheme::removeMnemonics(checkBoxLabel);
m_alert.suppressionButton.title = checkBoxLabel.toNSString();

View File

@ -1662,6 +1662,11 @@ void QMessageBoxPrivate::setVisible(bool visible)
if (q->testAttribute(Qt::WA_WState_ExplicitShowHide) && q->testAttribute(Qt::WA_WState_Hidden) != visible)
return;
// Last minute setup
if (autoAddOkButton)
q->addButton(QMessageBox::Ok);
detectEscapeButton();
if (canBeNativeDialog())
setNativeDialogVisible(visible);
@ -1706,13 +1711,7 @@ QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const
void QMessageBox::showEvent(QShowEvent *e)
{
Q_D(QMessageBox);
if (d->autoAddOkButton) {
addButton(Ok);
}
if (d->detailsButton)
addButton(d->detailsButton, QMessageBox::ActionRole);
d->clickedButton = nullptr;
d->detectEscapeButton();
d->updateSize();
#if QT_CONFIG(accessibility)