diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-10-20 13:19:43 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-30 18:16:21 +0200 |
commit | d982fdfca43165b31db7f7c3602b93f7197aeee8 (patch) | |
tree | 41ae57ce777d883abd909a092e91317341363440 /tests/auto/widgets/itemviews | |
parent | 3cacf1d1bd97c631465bb98edf6d00bff5bd5099 (diff) |
QHeaderView: allow un-sorting of models
If one clicks on a QHeaderView's section, the header view will
sort the view by the respective column/row. By clicking multiple
times, one is able to toggle the sorting between ascending
and descending. Something that is NOT possible to do however is to
un-sort the view -- that is, to restore the model's original
sorting. This must be done via code, by asking the header or the
view to sort by section -1.
This commit adds new property to QHeaderView to make it possible
to unsort models. Basically, the sort indicator becomes a tri-state:
sort ascending, sort descending, unsort (sort by column -1).
[ChangeLog][QtWidgets][QHeaderView] Added the sortIndicatorClearable
property. Setting this property allows the user to clear the sort
indicator on a section, resetting the model to its default ordering.
Change-Id: Ibf4e280b2086b75ccd64d619ea4d70816dc3529f
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r-- | tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index 83c6ed1c9f..610d99999a 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -156,6 +156,7 @@ private slots: void moveAndInsertSection(); void highlightSections(); void showSortIndicator(); + void clearSectionSorting(); void sortIndicatorTracking(); void removeAndInsertRow(); void unhideSection(); @@ -1333,6 +1334,75 @@ void tst_QHeaderView::showSortIndicator() // Don't assert baby :) } +void tst_QHeaderView::clearSectionSorting() +{ + QStandardItemModel m(4, 4); + QHeaderView h(Qt::Horizontal); + + QCOMPARE(h.sortIndicatorSection(), 0); + QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); + + h.setModel(&m); + h.setSectionsClickable(true); + h.setSortIndicatorShown(true); + h.setSortIndicator(-1, Qt::DescendingOrder); + h.show(); + + QVERIFY(QTest::qWaitForWindowExposed(&h)); + + QCOMPARE(h.sortIndicatorSection(), -1); + QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); + + QSignalSpy sectionClickedSpy(&h, &QHeaderView::sectionClicked); + QVERIFY(sectionClickedSpy.isValid()); + QCOMPARE(sectionClickedSpy.count(), 0); + + QSignalSpy sortIndicatorChangedSpy(&h, &QHeaderView::sortIndicatorChanged); + QVERIFY(sortIndicatorChangedSpy.isValid()); + QCOMPARE(sortIndicatorChangedSpy.count(), 0); + + enum { Count = 30 }; + + // normal behavior: clicking multiple times will just toggle the sort indicator + for (int i = 0; i < Count; ++i) { + QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); + QCOMPARE(sectionClickedSpy.count(), i + 1); + QCOMPARE(sortIndicatorChangedSpy.count(), i + 1); + QCOMPARE(h.sortIndicatorSection(), 0); + const auto expectedOrder = (i % 2) == 0 ? Qt::AscendingOrder : Qt::DescendingOrder; + QCOMPARE(h.sortIndicatorOrder(), expectedOrder); + } + + h.setSortIndicator(-1, Qt::DescendingOrder); + h.setSortIndicatorClearable(true); + QCOMPARE(h.sortIndicatorSection(), -1); + QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); + + sectionClickedSpy.clear(); + sortIndicatorChangedSpy.clear(); + + // clearing behavior: clicking multiple times will be tristate (asc, desc, nothing) + for (int i = 0; i < Count; ++i) { + QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); + QCOMPARE(sectionClickedSpy.count(), i + 1); + QCOMPARE(sortIndicatorChangedSpy.count(), i + 1); + switch (i % 3) { + case 0: + QCOMPARE(h.sortIndicatorSection(), 0); + QCOMPARE(h.sortIndicatorOrder(), Qt::AscendingOrder); + break; + case 1: + QCOMPARE(h.sortIndicatorSection(), 0); + QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); + break; + case 2: + QCOMPARE(h.sortIndicatorSection(), -1); + QCOMPARE(h.sortIndicatorOrder(), Qt::AscendingOrder); + break; + } + } +} + void tst_QHeaderView::sortIndicatorTracking() { QtTestModel model(10, 10); |