diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-06-28 15:52:09 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-06-29 18:49:46 +0200 |
commit | 4a4283e3e98d779e6eb6cb47d408fe4fd402cdf8 (patch) | |
tree | 5ce3a4287612862c735d805e029b907b3cbf11ad /src/widgets/dialogs | |
parent | 6da1ecc8c2b9f7fd488194b6e81b41a314b678d5 (diff) |
QMessageBox: Plumb native button clicks back to dialog's QAbstractButton
The native dialog helper is populated with buttons based on the message
box buttons, which are still represented as QAbstractButtons, either
by adding custom buttons, or by turning QMessageBox::StandardButton
into QAbstractButton.
To ensure as little difference as possible between the native and
non-native backed dialogs, we plumb the dialog helper clicks back
to the corresponding QAbstractButton. We were already doing this
partially for custom dialog buttons, but now we also do it for
standard buttons.
As the click() will plumb back to _q_buttonClicked, which calls
setClickedButton to finalize the dialog, we don't need to do
this explicitly anymore.
For custom buttons it now means the result() of the message box
will be the custom button's index, not its role, but the result
in this case is documented to be an opaque value, and it's best
to keep these the same for native and non-native dialogs.
Pick-to: 6.5 6.6
Change-Id: I5fe3b28e4c4ed879775610103cd1b591b3353f7e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/dialogs')
-rw-r--r-- | src/widgets/dialogs/qmessagebox.cpp | 34 | ||||
-rw-r--r-- | src/widgets/dialogs/qmessagebox.h | 2 |
2 files changed, 18 insertions, 18 deletions
diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index efc43ba396..d40d35bea0 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -178,7 +178,7 @@ public: void init(const QString &title = QString(), const QString &text = QString()); void setupLayout(); void _q_buttonClicked(QAbstractButton *); - void _q_clicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role); + void _q_helperClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role); void setClickedButton(QAbstractButton *button); QAbstractButton *findButton(int button0, int button1, int button2, int flags); @@ -496,23 +496,23 @@ void QMessageBoxPrivate::setClickedButton(QAbstractButton *button) finalize(resultCode, dialogCodeForButtonRole(q->buttonRole(button))); } -void QMessageBoxPrivate::_q_clicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role) +void QMessageBoxPrivate::_q_helperClicked(QPlatformDialogHelper::StandardButton helperButton, QPlatformDialogHelper::ButtonRole role) { + Q_UNUSED(role); Q_Q(QMessageBox); - if (button > QPlatformDialogHelper::LastButton) { - // It's a custom button, and the QPushButton in options is just a proxy - // for the button on the platform dialog. Simulate the user clicking it. - clickedButton = static_cast<QAbstractButton *>(options->customButton(button)->button); - Q_ASSERT(clickedButton); - clickedButton->click(); - close(role); - finalize(role, dialogCodeForButtonRole(q->buttonRole(clickedButton))); - } else { - clickedButton = q->button(QMessageBox::StandardButton(button)); - Q_ASSERT(clickedButton); - close(button); - finalize(button, dialogCodeForButtonRole(static_cast<QMessageBox::ButtonRole>(role))); - } + + // Map back to QAbstractButton, so that the message box behaves the same from + // the outside, regardless of whether it's backed by a native helper or not. + QAbstractButton *dialogButton = helperButton > QPlatformDialogHelper::LastButton ? + static_cast<QAbstractButton *>(options->customButton(helperButton)->button) : + q->button(QMessageBox::StandardButton(helperButton)); + + Q_ASSERT(dialogButton); + + // Simulate click by explicitly clicking the button. This will ensure that + // any logic of the button that responds to the click is respected, including + // plumbing back to _q_buttonClicked above based on the clicked() signal. + dialogButton->click(); } /*! @@ -2767,7 +2767,7 @@ void QMessageBoxPrivate::initHelper(QPlatformDialogHelper *h) { Q_Q(QMessageBox); QObject::connect(h, SIGNAL(clicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole)), - q, SLOT(_q_clicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole))); + q, SLOT(_q_helperClicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole))); auto *messageDialogHelper = static_cast<QPlatformMessageDialogHelper *>(h); QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged, q, diff --git a/src/widgets/dialogs/qmessagebox.h b/src/widgets/dialogs/qmessagebox.h index 76b23efc01..91fccd0d25 100644 --- a/src/widgets/dialogs/qmessagebox.h +++ b/src/widgets/dialogs/qmessagebox.h @@ -304,7 +304,7 @@ protected: private: Q_PRIVATE_SLOT(d_func(), void _q_buttonClicked(QAbstractButton *)) - Q_PRIVATE_SLOT(d_func(), void _q_clicked(QPlatformDialogHelper::StandardButton, QPlatformDialogHelper::ButtonRole)) + Q_PRIVATE_SLOT(d_func(), void _q_helperClicked(QPlatformDialogHelper::StandardButton, QPlatformDialogHelper::ButtonRole)) Q_DISABLE_COPY(QMessageBox) Q_DECLARE_PRIVATE(QMessageBox) |