From ee4ab8ce699bd0fd10fe664e053eb6f26da94268 Mon Sep 17 00:00:00 2001 From: Ismael Asensio Date: Fri, 6 Nov 2020 21:22:14 +0100 Subject: [PATCH] Iterate over a const copy list of containers This effectively reverts the mutable iterations approach on 2448f88c5f42d7a2040fcf3bcd3c5f2a2f62cd03, and fix crashes and ghost items when using the filter bar BUG: 428374 --- src/kitemviews/kfileitemmodel.cpp | 6 +++--- src/kitemviews/kfileitemmodelrolesupdater.cpp | 6 +++--- src/kitemviews/kitemlistview.cpp | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index c06202fd8..dbbd63a6a 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -919,9 +919,9 @@ void KFileItemModel::slotCompleted() // Note that the parent folder must be expanded before any of its subfolders become visible. // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet // -> we expand the first visible URL we find in m_restoredExpandedUrls. - QMutableSetIterator it(m_urlsToExpand); - while (it.hasNext()) { - const QUrl url = it.next(); + // Iterate over a const copy because items are deleted and inserted within the loop + const auto urlsToExpand = m_urlsToExpand; + for(const QUrl &url : urlsToExpand) { const int indexForUrl = index(url); if (indexForUrl >= 0) { m_urlsToExpand.remove(url); diff --git a/src/kitemviews/kfileitemmodelrolesupdater.cpp b/src/kitemviews/kfileitemmodelrolesupdater.cpp index 25e1a3685..f6f2815df 100644 --- a/src/kitemviews/kfileitemmodelrolesupdater.cpp +++ b/src/kitemviews/kfileitemmodelrolesupdater.cpp @@ -957,9 +957,9 @@ void KFileItemModelRolesUpdater::updateChangedItems() QList visibleChangedIndexes; QList invisibleChangedIndexes; - QMutableSetIterator it(m_changedItems); - while (it.hasNext()) { - const KFileItem item = it.next(); + // Iterate over a const copy because items are deleted within the loop + const auto changedItems = m_changedItems; + for (const KFileItem item : changedItems) { const int index = m_model->index(item); if (index < 0) { diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index 281edc3e9..f6e5e666b 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -1155,10 +1155,10 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges) QVector itemsToMove; // Remove all KItemListWidget instances that got deleted - QMutableHashIterator it(m_visibleItems); - while (it.hasNext()) { - it.next(); - KItemListWidget* widget = it.value(); + // Iterate over a const copy because the container is mutated within the loop + // directly and in `recycleWidget()` (https://bugs.kde.org/show_bug.cgi?id=428374) + const auto visibleItems = m_visibleItems; + for (KItemListWidget* widget : visibleItems) { const int i = widget->index(); if (i < firstRemovedIndex) { continue; -- 2.47.3