diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2022-11-14 22:14:52 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2022-11-15 12:27:39 +0100 |
commit | 523496984cf286f69dbbf1f3e19fdb7153adef1b (patch) | |
tree | db818383d401553ce113327b7caf763b912f02ee /src/widgets/dialogs | |
parent | 5827ee5ffc429591648019b6f19b72bf4207fe7e (diff) |
QMessageBox: Reflect native dialog visibility via QWidget state
When a QMessageBox has a native backend we want the QWidget state of
the dialog to still reflect whether the dialog is visible or not,
as this is used by various features of QMessageBox, such as resolving
the default escape button, for example.
It is also key to being able to test the dialog, since the auto test
relies on the observable widget state, and being able to send key events
to the widget to trigger hiding of the dialog.
The other QDialog subclasses with native dialog backends handle this
by showing the native dialog, and then setting Qt::WA_DontShowOnScreen
before calling QDialog::setVisible(), which results in the widget
state being "correct", but not showing any widget-based dialog.
QMessageBox however, did not implement setVisible(), and relied on
the QDialog implementation, which does not contain similar logic.
Ideally we want to clean up these overrides of setVisible(), and let
QDialog handle Qt::WA_DontShowOnScreen for all native dialogs, but
in the meantime we teach QMessageBox to do the same trick as the
other dialog subclasses.
Task-number: QTBUG-108153
Change-Id: I44c67cc9cd89660a703883bd57f351df70d7bdd3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/dialogs')
-rw-r--r-- | src/widgets/dialogs/qmessagebox.cpp | 17 | ||||
-rw-r--r-- | src/widgets/dialogs/qmessagebox.h | 2 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index d1af4b2842..4b20a183ce 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1556,6 +1556,23 @@ void QMessageBox::open(QObject *receiver, const char *member) QDialog::open(); } +void QMessageBox::setVisible(bool visible) +{ + if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) != visible) + return; + + Q_D(QMessageBox); + if (d->canBeNativeDialog()) + d->setNativeDialogVisible(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. + setAttribute(Qt::WA_DontShowOnScreen, d->nativeDialogInUse); + + QDialog::setVisible(visible); +} + /*! \since 4.5 diff --git a/src/widgets/dialogs/qmessagebox.h b/src/widgets/dialogs/qmessagebox.h index 4b1f5118c8..5d75ffd842 100644 --- a/src/widgets/dialogs/qmessagebox.h +++ b/src/widgets/dialogs/qmessagebox.h @@ -113,6 +113,8 @@ public: using QDialog::open; void open(QObject *receiver, const char *member); + void setVisible(bool visible) override; + QList<QAbstractButton *> buttons() const; ButtonRole buttonRole(QAbstractButton *button) const; |