From 4b47d64f151b3dc97993f32694d471cd6c9ea28f Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 28 Apr 2024 19:04:41 +0200 Subject: [PATCH] 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 (cherry picked from commit 0e5d5513279cff61673adc2ace5ed27202bbdc97) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qdialogbuttonbox.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/widgets/widgets/qdialogbuttonbox.cpp b/src/widgets/widgets/qdialogbuttonbox.cpp index 30ace89fa86..30c68ad18bb 100644 --- a/src/widgets/widgets/qdialogbuttonbox.cpp +++ b/src/widgets/widgets/qdialogbuttonbox.cpp @@ -647,12 +647,12 @@ void QDialogButtonBox::clear() d->standardButtonHash.clear(); for (int i = 0; i < NRoles; ++i) { QList &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); }