diff options
author | Thorvald Natvig <slicer@users.sourceforge.net> | 2009-09-01 15:13:14 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2009-09-02 15:10:57 (GMT) |
commit | c9eacfa1c791e2d228a3c8f0c119d02d7f46ee02 (patch) | |
tree | f4df981cbe76cadaa41291c61c278244ca01956a /src/gui/itemviews | |
parent | 3e741a2e9f8adb26c1b1d7ef5d80c40669ad3350 (diff) | |
download | Qt-c9eacfa1c791e2d228a3c8f0c119d02d7f46ee02.zip Qt-c9eacfa1c791e2d228a3c8f0c119d02d7f46ee02.tar.gz Qt-c9eacfa1c791e2d228a3c8f0c119d02d7f46ee02.tar.bz2 |
Make QTreeModel::ensureSorted() stable sort for items
Id you have numerous items with the same value in the sort column,
whenever you update one of them, they'll be placed at the head of
the list instead of staying in place.
For example, assume you have items
a b(1) b(2) b(3) b(4) c
(where all the b have the same value in the sort column)
If you now emitDataChanged from b(3), ensureSorted() will be called.
It will place b(3) in a list, and stable sort the list. It's just
the one item since there was only one item updated.
It than takes each item in the list, removes it's place from the
"full" list of items, then reinserts it at the earliest point (using qLowerBound).
End result:
a b(3) b(1) b(2) b(4) c
If you update all the items in the list (doing emitDataChanged()
for each), this has the effect of reversing all the items with
identical sort key.
This patch checks if the old row is within the lower and upper
bound of where the item might go, and if it is, simply reinserts
it in its old place.
Reviewed-by: Olivier Goffart
Merge-Request: 1393
Diffstat (limited to 'src/gui/itemviews')
-rw-r--r-- | src/gui/itemviews/qtreewidget.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/gui/itemviews/qtreewidget.cpp b/src/gui/itemviews/qtreewidget.cpp index 7d6439f..6a7b27c 100644 --- a/src/gui/itemviews/qtreewidget.cpp +++ b/src/gui/itemviews/qtreewidget.cpp @@ -622,6 +622,10 @@ void QTreeModel::ensureSorted(int column, Qt::SortOrder order, QTreeWidgetItem *item = lst.takeAt(oldRow); lit = sortedInsertionIterator(lit, lst.end(), order, item); int newRow = qMax(lit - lst.begin(), 0); + + if ((newRow < oldRow) && !(*item < *lst.at(oldRow - 1))) + newRow = oldRow; + lit = lst.insert(lit, item); if (newRow != oldRow) { // we are going to change the persistent indexes, so we need to prepare @@ -2074,7 +2078,7 @@ void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool { QTreeModel *model = (q->view ? qobject_cast<QTreeModel*>(q->view->model()) : 0); if (!model) - return; + return; model->sortItems(&q->children, column, order); if (climb) { QList<QTreeWidgetItem*>::iterator it = q->children.begin(); |