QDialogButtonBox: properly clear buttons in setStandardButtons()

When setting new buttons with setStandardButtons(), the old ones are
deleted by iterating over d->standardButtonHash. But this hash is
modified when a button is destroyed and therefore sometimes not all
buttons were deleted.
This amends df735d794fd2e545c18b9e345e833422bcd64329.

Fixes: QTBUG-123939
Pick-to: 6.5
Change-Id: I867086855cfde88a7b22a5579662f250b9db0042
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 0e5d5513279cff61673adc2ace5ed27202bbdc97)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-04-28 19:04:41 +02:00 committed by Qt Cherry-pick Bot
parent 9a504780b9
commit 4b47d64f15

View File

@ -647,12 +647,12 @@ void QDialogButtonBox::clear()
d->standardButtonHash.clear();
for (int i = 0; i < NRoles; ++i) {
QList<QAbstractButton *> &list = d->buttonLists[i];
while (list.size()) {
QAbstractButton *button = list.takeAt(0);
for (auto button : std::as_const(list)) {
QObjectPrivate::disconnect(button, &QAbstractButton::destroyed,
d, &QDialogButtonBoxPrivate::handleButtonDestroyed);
delete button;
}
list.clear();
}
}
@ -818,8 +818,8 @@ void QDialogButtonBox::setStandardButtons(StandardButtons buttons)
{
Q_D(QDialogButtonBox);
// Clear out all the old standard buttons, then recreate them.
qDeleteAll(d->standardButtonHash.keyBegin(), d->standardButtonHash.keyEnd());
d->standardButtonHash.clear();
const auto toDelete = std::exchange(d->standardButtonHash, {});
qDeleteAll(toDelete.keyBegin(), toDelete.keyEnd());
d->createStandardButtons(buttons);
}