From 3964d03f31f18ddbdfe26d6ee6b332a535e29acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 5 Jun 2024 12:35:06 +0200 Subject: [PATCH] Return button index for deprecated QMessageBox APIs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The static QMessageBox APIs taking button texts instead of standard buttons promises that the return value is 0, 1, or 2. After the change in b30121041c07b1b8613eaf624c9aa55a51001aef the return value of exec for custom buttons was changed (as the documentation says its an opaque value), which unfortunately affected the deprecated functions. To fix this, we use the index of the clicked button in the custom button list, to restore the previous behavior of the deprecated APIs. Fixes: QTBUG-125858 Pick-to: 6.7 Change-Id: I96d39e42b64e2b55eab07e2f15df71b94cfe3e6d Reviewed-by: Volker Hilsheimer Reviewed-by: Jan Arve Sæther (cherry picked from commit a428c6933565ee8368367534cf306ccc6957f5a5) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 5b151ea2d23dc3834180d3ec6495ac5d99cae550) --- src/widgets/dialogs/qmessagebox.cpp | 6 +++++- .../dialogs/qmessagebox/tst_qmessagebox.cpp | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index bf56b17f55..f24cd905cc 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -2158,7 +2158,11 @@ int QMessageBoxPrivate::showOldMessageBox(QWidget *parent, QMessageBox::Icon ico messageBox.setDefaultButton(static_cast(buttonList.value(defaultButtonNumber))); messageBox.setEscapeButton(buttonList.value(escapeButtonNumber)); - return messageBox.exec(); + messageBox.exec(); + + // Ignore exec return value and use button index instead, + // as that's what the documentation promises. + return buttonList.indexOf(messageBox.clickedButton()); } void QMessageBoxPrivate::retranslateStrings() diff --git a/tests/auto/widgets/dialogs/qmessagebox/tst_qmessagebox.cpp b/tests/auto/widgets/dialogs/qmessagebox/tst_qmessagebox.cpp index 94afff6e40..26a6245164 100644 --- a/tests/auto/widgets/dialogs/qmessagebox/tst_qmessagebox.cpp +++ b/tests/auto/widgets/dialogs/qmessagebox/tst_qmessagebox.cpp @@ -63,6 +63,7 @@ private slots: void hideNativeByDestruction(); void explicitDoneAfterButtonClicked(); + void legacyApiReturnValue(); void cleanup(); }; @@ -511,7 +512,7 @@ QT_WARNING_DISABLE_DEPRECATED // the button text versions closeHelper.start(Qt::Key_Enter); ret = QMessageBox::information(nullptr, "title", "text", "Yes", "No", QString(), 1); - COMPARE(ret, 3); // Custom button opaque result + COMPARE(ret, 1); QVERIFY(closeHelper.done()); #endif // QT_DEPRECATED_SINCE(6, 2) #undef COMPARE @@ -863,5 +864,21 @@ void tst_QMessageBox::explicitDoneAfterButtonClicked() QCOMPARE(rejectedSpy.size(), 3); } +void tst_QMessageBox::legacyApiReturnValue() +{ + ExecCloseHelper closeHelper; + for (int i = 0; i < 3; ++i) { + closeHelper.start(Qt::Key_Enter); +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + QCOMPARE(QMessageBox::warning(nullptr, "Title", "Text", + "Button 0", "Button 1", "Button 2", i), i); + closeHelper.start(Qt::Key_Escape); + QCOMPARE(QMessageBox::warning(nullptr, "Title", "Text", + "Button 0", "Button 1", "Button 2", 0, i), i); +QT_WARNING_POP + } +} + QTEST_MAIN(tst_QMessageBox) #include "tst_qmessagebox.moc"