summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Arne Vestbø <[email protected]>2024-06-05 12:35:06 +0200
committerQt Cherry-pick Bot <[email protected]>2024-06-05 21:58:07 +0000
commit5b151ea2d23dc3834180d3ec6495ac5d99cae550 (patch)
treee10e66253db4b15c8a43322e88d0b5370a29e00e
parentc8feef0bb5825102a397787366df74990cfdbf6e (diff)
Return button index for deprecated QMessageBox APIs
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.8 6.7 Change-Id: I96d39e42b64e2b55eab07e2f15df71b94cfe3e6d Reviewed-by: Volker Hilsheimer <[email protected]> Reviewed-by: Jan Arve Sæther <[email protected]> (cherry picked from commit a428c6933565ee8368367534cf306ccc6957f5a5) Reviewed-by: Qt Cherry-pick Bot <[email protected]>
-rw-r--r--src/widgets/dialogs/qmessagebox.cpp6
-rw-r--r--tests/auto/widgets/dialogs/qmessagebox/tst_qmessagebox.cpp19
2 files changed, 23 insertions, 2 deletions
diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp
index bf56b17f55f..f24cd905ccf 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<QPushButton *>(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 94afff6e408..26a62451643 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"