From 6da1ecc8c2b9f7fd488194b6e81b41a314b678d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 27 Jun 2023 19:27:05 +0200 Subject: [PATCH] QMessageBox: Emit accepted/rejected for native message boxes In d8bbb5ee0e60d44a70d29306e607a59caf7fe5bc we started respecting the button roles of buttons added to QMessageBox, by emitting accepted() and rejected() if the appropriate roles were found. Unfortunately this only touched the QMessageBoxPrivate::_q_buttonClicked code path, for non-native dialogs, leaving the code path for native dialogs in QMessageBoxPrivate::_q_clicked alone. We now follow the same approach for the native dialogs as for the non-native ones, by calling QMessageBoxPrivate::close() and QMessageBoxPrivate::finalize() explicitly, instead of going via QDialog::done(). This allows us to pass a dialog code to finalize(). One side effect of the original change was that overriding QDialog::done() for non-native dialogs no longer had any effect, as we were using lower level plumbing. Since we now align with the original change for native dialogs, we will adopt the same limitation, but this will be fixed in a follow up for both cases. The callback code for custom buttons in native dialogs could also use some alignment with the non-native path, but this is also left for a follow up. Fixes: QTBUG-113685 Pick-to: 6.5 6.6 Change-Id: Iea03a0007f884d6c7f11d2bd891446bdaa5ddc67 Reviewed-by: Volker Hilsheimer Reviewed-by: Erik Verbruggen --- src/widgets/dialogs/qmessagebox.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index 551663a711..efc43ba396 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -187,7 +187,7 @@ public: QAbstractButton *abstractButtonForId(int id) const; int execReturnCode(QAbstractButton *button); - int dialogCodeForButton(QAbstractButton *button) const; + int dialogCodeForButtonRole(QMessageBox::ButtonRole buttonRole) const; void detectEscapeButton(); void updateSize(); @@ -447,11 +447,9 @@ int QMessageBoxPrivate::execReturnCode(QAbstractButton *button) Returns 0 for RejectedRole and NoRole, 1 for AcceptedRole and YesRole, -1 otherwise */ -int QMessageBoxPrivate::dialogCodeForButton(QAbstractButton *button) const +int QMessageBoxPrivate::dialogCodeForButtonRole(QMessageBox::ButtonRole buttonRole) const { - Q_Q(const QMessageBox); - - switch (q->buttonRole(button)) { + switch (buttonRole) { case QMessageBox::AcceptRole: case QMessageBox::YesRole: return QDialog::Accepted; @@ -495,7 +493,7 @@ void QMessageBoxPrivate::setClickedButton(QAbstractButton *button) auto resultCode = execReturnCode(button); close(resultCode); - finalize(resultCode, dialogCodeForButton(button)); + finalize(resultCode, dialogCodeForButtonRole(q->buttonRole(button))); } void QMessageBoxPrivate::_q_clicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role) @@ -507,11 +505,13 @@ void QMessageBoxPrivate::_q_clicked(QPlatformDialogHelper::StandardButton button clickedButton = static_cast(options->customButton(button)->button); Q_ASSERT(clickedButton); clickedButton->click(); - q->done(role); + close(role); + finalize(role, dialogCodeForButtonRole(q->buttonRole(clickedButton))); } else { clickedButton = q->button(QMessageBox::StandardButton(button)); Q_ASSERT(clickedButton); - q->done(button); + close(button); + finalize(button, dialogCodeForButtonRole(static_cast(role))); } }