summaryrefslogtreecommitdiffstats
path: root/src/widgets/graphicsview/qgraphicsscene.cpp
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-03-17 15:23:55 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-03-18 21:56:19 +0100
commit5e566ab373bd6161f82b00c5a1f24bd210051140 (patch)
tree8e9cc13910b118a50c36a2c148e864e7d78fbb3b /src/widgets/graphicsview/qgraphicsscene.cpp
parent33d62de7c88551218124c8d7c91736366720a849 (diff)
When clearing selected items, iterate over a copy of the QSet
Selection change handlers of the items might call a method that implicitly recreates the selectedItems QSet, which then invalidates the iterators of the ranged for loop, resulting in crashes. Iterate over a copy of the set instead. Add a test case that crashen without the fix. Fixes: QTBUG-101651 Pick-to: 6.3 6.2 Change-Id: I6da6f4043fe1906b0186931a37283f635cb5a404 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp')
-rw-r--r--src/widgets/graphicsview/qgraphicsscene.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp
index f41ced1a38..d00d69c47c 100644
--- a/src/widgets/graphicsview/qgraphicsscene.cpp
+++ b/src/widgets/graphicsview/qgraphicsscene.cpp
@@ -2285,9 +2285,11 @@ void QGraphicsScene::clearSelection()
// Disable emitting selectionChanged
++d->selectionChanging;
- bool changed = !d->selectedItems.isEmpty();
+ // iterate over a copy, as clearing selection might invalidate selectedItems
+ const auto selectedItems = d->selectedItems;
+ bool changed = !selectedItems.isEmpty();
- for (QGraphicsItem *item : qAsConst(d->selectedItems))
+ for (QGraphicsItem *item : selectedItems)
item->setSelected(false);
d->selectedItems.clear();