From 3c449f7da5b606a83cabc35ccff9c71826561b02 Mon Sep 17 00:00:00 2001 From: Libor Tomsik Date: Fri, 25 Sep 2015 10:04:21 +0200 Subject: [PATCH] Fix selected items after sorting in QTableWidget OldPersistentIndexes store selected items during sort operation. They were wrongly taken from static list of indexes. This change takes them from parent (QTableWidget) who maintains the list of selected segments. Task-number: QTBUG-48408 Change-Id: Ie1bc4071a275dd76d113d883ab30ccd4cb1fa625 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/widgets/itemviews/qtablewidget.cpp | 12 +++++++----- .../qtablewidget/tst_qtablewidget.cpp | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index f0c7ac0d32..a10b95f701 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -555,9 +555,7 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order, LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); std::stable_sort(sorting.begin(), sorting.end(), compare); - - QModelIndexList oldPersistentIndexes = persistentIndexList(); - QModelIndexList newPersistentIndexes = oldPersistentIndexes; + QModelIndexList oldPersistentIndexes, newPersistentIndexes; QVector newTable = tableItems; QVector newVertical = verticalHeaderItems; QVector colItems = columnItems(column); @@ -573,7 +571,12 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order, newRow = oldRow; vit = colItems.insert(vit, item); if (newRow != oldRow) { - changed = true; + if (!changed) { + emit layoutAboutToBeChanged(); + oldPersistentIndexes = persistentIndexList(); + newPersistentIndexes = oldPersistentIndexes; + changed = true; + } // move the items @ oldRow to newRow int cc = columnCount(); QVector rowItems(cc); @@ -600,7 +603,6 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order, } if (changed) { - emit layoutAboutToBeChanged(); tableItems = newTable; verticalHeaderItems = newVertical; changePersistentIndexList(oldPersistentIndexes, diff --git a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp index 6d2b2ea964..ea31fd19dd 100644 --- a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp +++ b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp @@ -94,6 +94,7 @@ private slots: void task262056_sortDuplicate(); void itemWithHeaderItems(); void mimeData(); + void selectedRowAfterSorting(); private: QTableWidget *testWidget; @@ -1565,5 +1566,23 @@ void tst_QTableWidget::mimeData() delete data2; } +void tst_QTableWidget::selectedRowAfterSorting() +{ + TestTableWidget table(3,3); + table.setSelectionBehavior(QAbstractItemView::SelectRows); + for (int r = 0; r < 3; r++) + for (int c = 0; c < 3; c++) + table.setItem(r,c,new QTableWidgetItem(QStringLiteral("0"))); + QHeaderView *localHorizontalHeader = table.horizontalHeader(); + localHorizontalHeader->setSortIndicator(1,Qt::DescendingOrder); + table.setProperty("sortingEnabled",true); + table.selectRow(1); + table.item(1,1)->setText("9"); + QCOMPARE(table.selectedItems().count(),3); + foreach (QTableWidgetItem *item, table.selectedItems()) { + QCOMPARE(item->row(),0); + } +} + QTEST_MAIN(tst_QTableWidget) #include "tst_qtablewidget.moc"