QTree/TableView: allow to reset the sort order to natural sorting

QTreeView allowed to set the sort column to -1 which shows the data in
it's natural order (when the model supports it). This functionality was
removed during the porting away from the deprecated sortByColumn(int)
functionality done in d0f909f8db
Readd the functionality and also allow it for QTableView.

Fixes: QTBUG-77419
Change-Id: I96b0c09ab9da36ca0a9de58fe0f37e2c56b1d51b
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Christian Ehrlicher 2019-08-10 18:21:59 +02:00
parent fdffa035ba
commit de26ea6a7f
4 changed files with 54 additions and 25 deletions

View File

@ -3187,14 +3187,18 @@ void QTableView::sortByColumn(int column)
/*!
\since 4.2
Sorts the model by the values in the given \a column in the given \a order.
Sorts the model by the values in the given \a column and \a order.
\a column may be -1, in which case no sort indicator will be shown
and the model will return to its natural, unsorted order. Note that not
all models support this and may even crash in this case.
\sa sortingEnabled
*/
void QTableView::sortByColumn(int column, Qt::SortOrder order)
{
Q_D(QTableView);
if (column < 0)
if (column < -1)
return;
// If sorting is enabled it will emit a signal connected to
// _q_sortIndicatorChanged, which then actually sorts

View File

@ -2618,7 +2618,7 @@ void QTreeView::sortByColumn(int column)
/*!
\since 4.2
Sets the model up for sorting by the values in the given \a column and \a order.
Sorts the model by the values in the given \a column and \a order.
\a column may be -1, in which case no sort indicator will be shown
and the model will return to its natural, unsorted order. Note that not
@ -2629,7 +2629,7 @@ void QTreeView::sortByColumn(int column)
void QTreeView::sortByColumn(int column, Qt::SortOrder order)
{
Q_D(QTreeView);
if (column < 0)
if (column < -1)
return;
// If sorting is enabled it will emit a signal connected to
// _q_sortIndicatorChanged, which then actually sorts

View File

@ -4248,32 +4248,42 @@ void tst_QTableView::task191545_dragSelectRows()
void tst_QTableView::task234926_setHeaderSorting()
{
QStringListModel model;
QStringList data;
data << "orange" << "apple" << "banana" << "lemon" << "pumpkin";
QSortFilterProxyModel sfpm; // default QStandardItemModel does not support 'unsorted' state
sfpm.setSourceModel(&model);
const QStringList data({"orange", "apple", "banana", "lemon", "pumpkin"});
QStringList sortedDataA = data;
QStringList sortedDataD = data;
std::sort(sortedDataA.begin(), sortedDataA.end());
std::sort(sortedDataD.begin(), sortedDataD.end(), std::greater<QString>());
model.setStringList(data);
QTableView view;
view.setModel(&model);
// view.show();
view.setModel(&sfpm);
QTRY_COMPARE(model.stringList(), data);
view.setSortingEnabled(true);
view.sortByColumn(0, Qt::AscendingOrder);
QTRY_COMPARE(model.stringList() , sortedDataA);
for (int i = 0; i < sortedDataA.size(); ++i)
QCOMPARE(view.model()->data(view.model()->index(i, 0)).toString(), sortedDataA.at(i));
view.horizontalHeader()->setSortIndicator(0, Qt::DescendingOrder);
QTRY_COMPARE(model.stringList() , sortedDataD);
for (int i = 0; i < sortedDataD.size(); ++i)
QCOMPARE(view.model()->data(view.model()->index(i, 0)).toString(), sortedDataD.at(i));
QHeaderView *h = new QHeaderView(Qt::Horizontal);
h->setModel(&model);
view.setHorizontalHeader(h);
h->setSortIndicator(0, Qt::AscendingOrder);
QTRY_COMPARE(model.stringList() , sortedDataA);
for (int i = 0; i < sortedDataA.size(); ++i)
QCOMPARE(view.model()->data(view.model()->index(i, 0)).toString(), sortedDataA.at(i));
h->setSortIndicator(0, Qt::DescendingOrder);
QTRY_COMPARE(model.stringList() , sortedDataD);
for (int i = 0; i < sortedDataD.size(); ++i)
QCOMPARE(view.model()->data(view.model()->index(i, 0)).toString(), sortedDataD.at(i));
view.sortByColumn(-1, Qt::AscendingOrder);
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), -1);
for (int i = 0; i < data.size(); ++i)
QCOMPARE(view.model()->data(view.model()->index(i, 0)).toString(), data.at(i));
}
void tst_QTableView::taskQTBUG_5062_spansInconsistency()

View File

@ -2783,25 +2783,40 @@ void tst_QTreeView::sortByColumn()
QFETCH(bool, sortingEnabled);
QTreeView view;
QStandardItemModel model(4,2);
model.setItem(0,0,new QStandardItem("b"));
model.setItem(1,0,new QStandardItem("d"));
model.setItem(2,0,new QStandardItem("c"));
model.setItem(3,0,new QStandardItem("a"));
model.setItem(0,1,new QStandardItem("e"));
model.setItem(1,1,new QStandardItem("g"));
model.setItem(2,1,new QStandardItem("h"));
model.setItem(3,1,new QStandardItem("f"));
QSortFilterProxyModel sfpm; // default QStandardItemModel does not support 'unsorted' state
sfpm.setSourceModel(&model);
model.setItem(0, 0, new QStandardItem("b"));
model.setItem(1, 0, new QStandardItem("d"));
model.setItem(2, 0, new QStandardItem("c"));
model.setItem(3, 0, new QStandardItem("a"));
model.setItem(0, 1, new QStandardItem("e"));
model.setItem(1, 1, new QStandardItem("g"));
model.setItem(2, 1, new QStandardItem("h"));
model.setItem(3, 1, new QStandardItem("f"));
view.setSortingEnabled(sortingEnabled);
view.setModel(&model);
view.setModel(&sfpm);
view.sortByColumn(1, Qt::DescendingOrder);
QCOMPARE(view.header()->sortIndicatorSection(), 1);
QCOMPARE(view.model()->data(view.model()->index(0,1)).toString(), QString::fromLatin1("h"));
QCOMPARE(view.model()->data(view.model()->index(1,1)).toString(), QString::fromLatin1("g"));
QCOMPARE(view.model()->data(view.model()->index(0, 0)).toString(), QString::fromLatin1("c"));
QCOMPARE(view.model()->data(view.model()->index(1, 0)).toString(), QString::fromLatin1("d"));
QCOMPARE(view.model()->data(view.model()->index(0, 1)).toString(), QString::fromLatin1("h"));
QCOMPARE(view.model()->data(view.model()->index(1, 1)).toString(), QString::fromLatin1("g"));
view.sortByColumn(0, Qt::AscendingOrder);
QCOMPARE(view.header()->sortIndicatorSection(), 0);
QCOMPARE(view.model()->data(view.model()->index(0,0)).toString(), QString::fromLatin1("a"));
QCOMPARE(view.model()->data(view.model()->index(1,0)).toString(), QString::fromLatin1("b"));
QCOMPARE(view.model()->data(view.model()->index(0, 0)).toString(), QString::fromLatin1("a"));
QCOMPARE(view.model()->data(view.model()->index(1, 0)).toString(), QString::fromLatin1("b"));
QCOMPARE(view.model()->data(view.model()->index(0, 1)).toString(), QString::fromLatin1("f"));
QCOMPARE(view.model()->data(view.model()->index(1, 1)).toString(), QString::fromLatin1("e"));
view.sortByColumn(-1, Qt::AscendingOrder);
QCOMPARE(view.header()->sortIndicatorSection(), -1);
QCOMPARE(view.model()->data(view.model()->index(0, 0)).toString(), QString::fromLatin1("b"));
QCOMPARE(view.model()->data(view.model()->index(1, 0)).toString(), QString::fromLatin1("d"));
QCOMPARE(view.model()->data(view.model()->index(0, 1)).toString(), QString::fromLatin1("e"));
QCOMPARE(view.model()->data(view.model()->index(1, 1)).toString(), QString::fromLatin1("g"));
}
/*