From d71b73c145a35a84547918abe4b0916a7ced6a1e Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Thu, 23 Nov 2023 13:05:58 +0100 Subject: [PATCH] Pass correct default button in QMessageBox::showNewMessageBox() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit showNewMessageBox() shows an "old" message box, if a default button argument was passed and the buttons argument doesn't contain a default button. It passed the int value of defaultButton to showOldMessageBox, where it was interpreted as a normal button. The StandardButton::Default flag was not set on the default button. This relied on the QDialogButtonBox owned by QMessageBox to show the expected default button by co-incidence. As this was not always the case, tst_QMessageBox::staticSourceCompat() even tested wrong expected results. => Add the Default flag to the default button, before passing it as an int value. => As a drive-by, - replace c-style casting with static casting. - add braces to multi-line if clause. Task-number: QTBUG-118489 Pick-to: 6.7 6.6 6.5 Change-Id: I9cf93c8f93d6ab80e7be5ab25e56bc59d3d6209c Reviewed-by: Tor Arne Vestbø --- src/widgets/dialogs/qmessagebox.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index e0edfe4e03..f55113b3ca 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1729,11 +1729,14 @@ static QMessageBox::StandardButton showNewMessageBox(QWidget *parent, { // necessary for source compatibility with Qt 4.0 and 4.1 // handles (Yes, No) and (Yes|Default, No) - if (defaultButton && !(buttons & defaultButton)) - return (QMessageBox::StandardButton) - QMessageBoxPrivate::showOldMessageBox(parent, icon, title, - text, int(buttons), - int(defaultButton), 0); + if (defaultButton && !(buttons & defaultButton)) { + const int defaultButtons = defaultButton | QMessageBox::Default; + const int otherButtons = static_cast(buttons); + const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title, + text, otherButtons, + defaultButtons, 0); + return static_cast(ret); + } QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent); QDialogButtonBox *buttonBox = msgBox.findChild();