Pass correct default button in QMessageBox::showNewMessageBox()

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.6 6.5
Change-Id: I9cf93c8f93d6ab80e7be5ab25e56bc59d3d6209c
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit d71b73c145a35a84547918abe4b0916a7ced6a1e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Axel Spoerl 2023-11-23 13:05:58 +01:00
parent 0ea24c99eb
commit a8d20fe2db

View File

@ -1743,11 +1743,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<int>(buttons);
const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
text, otherButtons,
defaultButtons, 0);
return static_cast<QMessageBox::StandardButton>(ret);
}
QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();