From 148add03f3bc2e1e52509500afa80793d1524dc8 Mon Sep 17 00:00:00 2001 From: Ali Kianian Date: Thu, 18 Apr 2024 10:27:13 +0300 Subject: [PATCH] Enable resetting a sort model by descending order By this change, sort(any_negative_number) will stay unchanged, and it unsorts the model so that the order will be the same as the source model. But when the second argument of the sort is set to DescendingOrder deliberately, the result will be the same as the source model, but in reverse order. [ChangeLog][QTCore][QSortFilterProxyModel] QSortFilterProxyModel.sort sorts the proxy by the source model index descending if the sort column is less than zero and the order is descending. Fixes: QTBUG-124863 Change-Id: I73f9d3b500a929efa969cf24ef3d8fe758167645 Reviewed-by: Volker Hilsheimer --- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 11 ++++++++--- .../tst_qsortfilterproxymodel.cpp | 11 +++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index a9ead2e1eb..a5284dbad4 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -688,8 +688,10 @@ void QSortFilterProxyModelPrivate::sort_source_rows( QSortFilterProxyModelGreaterThan gt(source_sort_column, source_parent, model, q); std::stable_sort(source_rows.begin(), source_rows.end(), gt); } - } else { // restore the source model order - std::stable_sort(source_rows.begin(), source_rows.end()); + } else if (sort_order == Qt::AscendingOrder) { + std::stable_sort(source_rows.begin(), source_rows.end(), std::less{}); + } else { + std::stable_sort(source_rows.begin(), source_rows.end(), std::greater{}); } } @@ -2490,7 +2492,10 @@ QSize QSortFilterProxyModel::span(const QModelIndex &index) const } /*! - \reimp + \reimp + Sorts the model by \a column in the given \a order. + If the sort \a column is less than zero, the model will be sorted by source model row + in the given \a order. */ void QSortFilterProxyModel::sort(int column, Qt::SortOrder order) { diff --git a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp index 5a06a4a605..0e027461aa 100644 --- a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp @@ -247,13 +247,16 @@ void tst_QSortFilterProxyModel::sort() QCOMPARE(m_proxy->data(index, Qt::DisplayRole).toString(), expected.at(row)); } - // restore the unsorted order - m_proxy->sort(-1); + // restore the unsorted order in the given order + m_proxy->sort(-1, sortOrder); - // make sure the proxy is unsorted again + // make sure the proxy is sorted by source row in the given order + int sourceIndex = sortOrder == Qt::AscendingOrder ? 0 : initial.size() - 1; + int adjustmentValue = sortOrder == Qt::AscendingOrder ? 1 : -1; for (int row = 0; row < m_proxy->rowCount(QModelIndex()); ++row) { QModelIndex index = m_proxy->index(row, 0, QModelIndex()); - QCOMPARE(m_proxy->data(index, Qt::DisplayRole).toString(), initial.at(row)); + QCOMPARE(m_proxy->data(index, Qt::DisplayRole).toString(), initial.at(sourceIndex)); + sourceIndex += adjustmentValue; } }