QMessageBox: Emit accepted/rejected for native message boxes

In d8bbb5ee0e 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 <volker.hilsheimer@qt.io>
Reviewed-by: Erik Verbruggen <erik.verbruggen@me.com>
This commit is contained in:
Tor Arne Vestbø 2023-06-27 19:27:05 +02:00
parent cd9ae49962
commit 6da1ecc8c2

View File

@ -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<QAbstractButton *>(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<QMessageBox::ButtonRole>(role)));
}
}