From 19030e2af098e319e3c3f3883c51e28364bf3ccf Mon Sep 17 00:00:00 2001 From: Pierre Rossi Date: Tue, 13 Oct 2009 16:26:10 +0200 Subject: Sorting bug in QTableView fix When the DisplayRole is identical for two or more items, any modification of one item (e.g. backgroundColorRole) may trigger a re-ordering, which is obviously not an behaviour to be expected. Same fix as the one used for QTreewidget in c9eacfa1c791e2d228a3c8f0c119d02d7f46ee02. Task-number: QTBUG-4856 Task-number: 262056 Reviewed-by: Olivier Goffart --- src/gui/itemviews/qtablewidget.cpp | 2 ++ tests/auto/qtablewidget/tst_qtablewidget.cpp | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/gui/itemviews/qtablewidget.cpp b/src/gui/itemviews/qtablewidget.cpp index a086498..65758b8 100644 --- a/src/gui/itemviews/qtablewidget.cpp +++ b/src/gui/itemviews/qtablewidget.cpp @@ -571,6 +571,8 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order, colItems.remove(oldRow); vit = sortedInsertionIterator(vit, colItems.end(), order, item); int newRow = qMax((int)(vit - colItems.begin()), 0); + if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1))) + newRow = oldRow; vit = colItems.insert(vit, item); if (newRow != oldRow) { changed = true; diff --git a/tests/auto/qtablewidget/tst_qtablewidget.cpp b/tests/auto/qtablewidget/tst_qtablewidget.cpp index 5aa2d1d..b85851f 100644 --- a/tests/auto/qtablewidget/tst_qtablewidget.cpp +++ b/tests/auto/qtablewidget/tst_qtablewidget.cpp @@ -102,6 +102,7 @@ private slots: void cellWidget(); void task231094(); void task219380_removeLastRow(); + void task262056_sortDuplicate(); private: QTableWidget *testWidget; @@ -1474,6 +1475,26 @@ void tst_QTableWidget::task219380_removeLastRow() QCOMPARE(testWidget->cellWidget(18, 0)->geometry(), testWidget->visualItemRect(&item)); } +void tst_QTableWidget::task262056_sortDuplicate() +{ + testWidget->setColumnCount(2); + testWidget->setRowCount(8); + testWidget->setSortingEnabled(true); + QStringList items = (QStringList() << "AAA" << "BBB" << "CCC" << "CCC" << "DDD"\ + << "EEE" << "FFF" << "GGG"); + for (int i = 0; i<8; i++ ) { + QTableWidgetItem *twi = new QTableWidgetItem(items.at(i)); + testWidget->setItem(i,0,twi); + testWidget->setItem(i,1,new QTableWidgetItem(QString("item %1").arg(i))); + } + testWidget->sortItems(0, Qt::AscendingOrder); + QSignalSpy layoutChangedSpy(testWidget->model(), SIGNAL(layoutChanged())); + testWidget->item(3,0)->setBackgroundColor(Qt::red); + + QCOMPARE(layoutChangedSpy.count(),0); + +} + QTEST_MAIN(tst_QTableWidget) #include "tst_qtablewidget.moc" -- cgit v0.12