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.7 6.6 6.5
Change-Id: I9cf93c8f93d6ab80e7be5ab25e56bc59d3d6209c
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Axel Spoerl 2023-11-23 13:05:58 +01:00 committed by Tor Arne Vestbø
parent fd710fbba3
commit d71b73c145
1 changed files with 8 additions and 5 deletions

View File

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