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.7
Change-Id: I96d39e42b64e2b55eab07e2f15df71b94cfe3e6d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
(cherry picked from commit a428c6933565ee8368367534cf306ccc6957f5a5)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 5b151ea2d23dc3834180d3ec6495ac5d99cae550)
This commit is contained in:
Tor Arne Vestbø 2024-06-05 12:35:06 +02:00 committed by Qt Cherry-pick Bot
parent f51051bb6b
commit 3964d03f31
2 changed files with 23 additions and 2 deletions

View File

@ -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()

View File

@ -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"