From 1dcfb09c5bf431bf8b065ac038bd1fc618a68f96 Mon Sep 17 00:00:00 2001 From: Luca Beldi Date: Tue, 20 Jul 2021 15:49:33 +0100 Subject: emit layoutAboutToBeChanged timely layoutAboutToBeChanged must be called before persistentIndexList as the user might create persistent indexes as a response to the signal Fixes: QTBUG-93466 Pick-to: 6.2 5.15 Change-Id: I73c24501f536ef9b6092c3374821497f0a8f0de4 Reviewed-by: David Faure --- src/widgets/itemviews/qlistwidget.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/widgets/itemviews') diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index ca7439b08c..f04cfe45d9 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -373,7 +373,7 @@ void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int en if (column != 0) return; - int count = end - start + 1; + const int count = end - start + 1; QList> sorting(count); for (int i = 0; i < count; ++i) { sorting[i].first = items.at(start + i); @@ -399,7 +399,12 @@ void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int en int newRow = qMax(lit - tmp.begin(), 0); lit = tmp.insert(lit, item); if (newRow != oldRow) { - changed = true; + if (!changed) { + emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint); + oldPersistentIndexes = persistentIndexList(); + newPersistentIndexes = oldPersistentIndexes; + changed = true; + } for (int j = i + 1; j < count; ++j) { int otherRow = sorting.at(j).second; if (oldRow < otherRow && newRow >= otherRow) @@ -425,10 +430,9 @@ void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int en } if (changed) { - emit layoutAboutToBeChanged(); items = tmp; changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes); - emit layoutChanged(); + emit layoutChanged({}, QAbstractItemModel::VerticalSortHint); } } -- cgit v1.2.3