summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2019-08-10 18:21:59 +0200
committerChristian Ehrlicher <ch.ehrlicher@gmx.de>2019-08-13 18:22:14 +0200
commitde26ea6a7ff921995b6229f1f683821adb95e973 (patch)
treee6c34259817c65bc3010c337b75e456c497666a9
parentfdffa035ba261d374101d230c0a884725f34e362 (diff)
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 d0f909f8dbdd8594b0d950822f0e7ab8728da513 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>
-rw-r--r--src/widgets/itemviews/qtableview.cpp8
-rw-r--r--src/widgets/itemviews/qtreeview.cpp4
-rw-r--r--tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp26
-rw-r--r--tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp41
4 files changed, 54 insertions, 25 deletions
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index d4a6433c4d..b05662f6bc 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -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
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index 55b10d13c1..e9228edcda 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -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
diff --git a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
index b1ddc6e7a2..c8eecd3693 100644
--- a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
+++ b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
@@ -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()
diff --git a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
index c31de2ba22..2a1163ec66 100644
--- a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
+++ b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
@@ -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"));
}
/*