summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-11-25 22:03:38 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2020-12-01 18:43:35 +0000
commit8b7d6a14fcae3bcd201df6ddb8eb6f17258cb03b (patch)
tree6f2d3cc584e61485eff1a89a4a48923096fe30b1 /tests
parentb5ecdce605685996a5e280edd164f8853f8326df (diff)
QHeaderView: fix spurious sorting
QHeaderView sorting may be triggered when the user performs some mouse interactions that should really not result in sorting. Generally speaking, this happens when the user: * presses on a non-movable section (A) * moves on another section (B) * releases on that section resulting in B becoming sorted / flipping sorting. (Non-movable is required, otherwise dragging would cause section moving, not sorting.) To make the matter worse, QHeaderView doesn't check that the release happens within its geometry. This makes sense when moving sections: one is able to drag a section horizontally/vertically even if the mouse leaves the QHeaderView. But when not moving sections, this means that one can * press on section (A), * move the mouse anywhere vertically (for a horizontal bar, mut.mut for a vertical) above or below another section (B), that is, outside QHeaderView's geometry * release the mouse and cause B to be sorted. Fix it by 1) remembering which one was the section that the user originally clicked on; that's the only one that can possibly become sorted (if we're not moving and other conditions hold). No other variable seemed to remember this. 2) on release, check that it happens within that section's geometry. If so, sort. Change-Id: Icfb67662221efbde019711f933781ee1e7d9ac43 Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> (cherry picked from commit b5b2640a65de20d05890fa9e6462bb7b88f83964) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp135
1 files changed, 135 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..784c96c58d 100644
--- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
+++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
@@ -216,6 +216,7 @@ private slots:
void QTBUG75615_sizeHintWithStylesheet();
void ensureNoIndexAtLength();
void offsetConsistent();
+ void sectionsDontSortWhenNotClickingInThem();
void initialSortOrderRole();
@@ -2607,6 +2608,140 @@ void tst_QHeaderView::offsetConsistent()
QVERIFY(offset2 > offset1);
}
+void tst_QHeaderView::sectionsDontSortWhenNotClickingInThem()
+{
+ QTableView qtv;
+ QStandardItemModel amodel(1000, 4);
+ qtv.setModel(&amodel);
+ QHeaderView *hv = qtv.horizontalHeader();
+ hv->setSectionsClickable(true);
+ hv->setFirstSectionMovable(true);
+ hv->setSectionsMovable(false);
+
+ enum { DefaultYOffset = 5, OutOfRangeYOffset = 10000 };
+
+ const auto pressOnSection = [&](int section, int yOffset = DefaultYOffset)
+ {
+ QTest::mousePress(hv->viewport(), Qt::LeftButton, Qt::NoModifier,
+ QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset));
+ };
+ const auto moveOntoSection = [&](int section, int yOffset = DefaultYOffset)
+ {
+ QTest::mouseMove(hv->viewport(),
+ QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset));
+ };
+ const auto releaseOnSection = [&](int section, int yOffset = DefaultYOffset)
+ {
+ QTest::mouseRelease(hv->viewport(), Qt::LeftButton, Qt::NoModifier,
+ QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset));
+ };
+
+ hv->setSortIndicator(-1, Qt::AscendingOrder);
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ releaseOnSection(0);
+ // RESULT: sorting
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ hv->setSortIndicator(-1, Qt::AscendingOrder);
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ moveOntoSection(1);
+ releaseOnSection(1);
+ // RESULT: no sorting
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ moveOntoSection(1);
+ moveOntoSection(2);
+ releaseOnSection(2);
+ // RESULT: no sorting
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ moveOntoSection(1);
+ moveOntoSection(0);
+ releaseOnSection(0);
+ // RESULT: sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ pressOnSection(0);
+ moveOntoSection(1);
+ releaseOnSection(1);
+ // RESULT: no change, still sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ auto sortOrder = hv->sortIndicatorOrder();
+ pressOnSection(1);
+ moveOntoSection(0);
+ releaseOnSection(0);
+ // RESULT: no change, still sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+ QCOMPARE(hv->sortIndicatorOrder(), sortOrder);
+
+ pressOnSection(1);
+ moveOntoSection(0);
+ moveOntoSection(1);
+ releaseOnSection(1);
+ // RESULT: sorting by 1
+ QCOMPARE(hv->sortIndicatorSection(), 1);
+
+ pressOnSection(1);
+ moveOntoSection(0);
+ releaseOnSection(0);
+ // RESULT: no change, still sorting by 1
+ QCOMPARE(hv->sortIndicatorSection(), 1);
+
+ hv->setSortIndicator(-1, Qt::AscendingOrder);
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ releaseOnSection(0, OutOfRangeYOffset);
+ // RESULT: no sorting
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ moveOntoSection(0, OutOfRangeYOffset);
+ releaseOnSection(0, OutOfRangeYOffset);
+ // RESULT: no sorting
+ QCOMPARE(hv->sortIndicatorSection(), -1);
+
+ pressOnSection(0);
+ moveOntoSection(0, OutOfRangeYOffset);
+ moveOntoSection(0);
+ releaseOnSection(0);
+ // RESULT: sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ pressOnSection(1);
+ releaseOnSection(1, OutOfRangeYOffset);
+ // RESULT: no change, still sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ pressOnSection(1);
+ moveOntoSection(1, OutOfRangeYOffset);
+ releaseOnSection(1, OutOfRangeYOffset);
+ // RESULT: no change, still sorting by 0
+ QCOMPARE(hv->sortIndicatorSection(), 0);
+
+ pressOnSection(1);
+ moveOntoSection(1, OutOfRangeYOffset);
+ moveOntoSection(1);
+ releaseOnSection(1);
+ // RESULT: sorting by 1
+ QCOMPARE(hv->sortIndicatorSection(), 1);
+
+ pressOnSection(2);
+ moveOntoSection(1);
+ moveOntoSection(2);
+ moveOntoSection(2, OutOfRangeYOffset);
+ releaseOnSection(2, OutOfRangeYOffset);
+ // RESULT: no change, still sorting by 1
+ QCOMPARE(hv->sortIndicatorSection(), 1);
+}
+
void tst_QHeaderView::initialSortOrderRole()
{
QTableView view; // ### Shadowing member view (of type QHeaderView)