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 <volker.hilsheimer@qt.io>
bb10
Ali Kianian 2024-04-18 10:27:13 +03:00
parent f9a994b86a
commit 148add03f3
2 changed files with 15 additions and 7 deletions

View File

@ -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)
{

View File

@ -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;
}
}