diff options
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 30 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview_p.h | 2 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp | 135 |
3 files changed, 161 insertions, 6 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index db594edd53..becf1e1a24 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -2563,7 +2563,7 @@ void QHeaderView::mousePressEvent(QMouseEvent *e) int handle = d->sectionHandleAt(pos); d->originalSize = -1; // clear the stored original size if (handle == -1) { - d->pressed = logicalIndexAt(pos); + d->firstPressed = d->pressed = logicalIndexAt(pos); if (d->clickableSections) emit sectionPressed(d->pressed); @@ -2611,7 +2611,7 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e) // just before the mouseReleaseEvent and resets the state. This prevents // column dragging from working. So this code is disabled under Cocoa. d->state = QHeaderViewPrivate::NoState; - d->pressed = -1; + d->firstPressed = d->pressed = -1; } switch (d->state) { case QHeaderViewPrivate::ResizeSection: { @@ -2740,9 +2740,27 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e) case QHeaderViewPrivate::NoState: if (d->clickableSections) { int section = logicalIndexAt(pos); - if (section != -1 && section == d->pressed) { - d->flipSortIndicator(section); - emit sectionClicked(section); + if (section != -1 && section == d->firstPressed) { + QRect firstPressedSectionRect; + switch (d->orientation) { + case Qt::Horizontal: + firstPressedSectionRect.setRect(sectionViewportPosition(d->firstPressed), + 0, + sectionSize(d->firstPressed), + d->viewport->height()); + break; + case Qt::Vertical: + firstPressedSectionRect.setRect(0, + sectionViewportPosition(d->firstPressed), + d->viewport->width(), + sectionSize(d->firstPressed)); + break; + }; + + if (firstPressedSectionRect.contains(e->position().toPoint())) { + d->flipSortIndicator(section); + emit sectionClicked(section); + } } if (d->pressed != -1) updateSection(d->pressed); @@ -2756,7 +2774,7 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e) break; } d->state = QHeaderViewPrivate::NoState; - d->pressed = -1; + d->firstPressed = d->pressed = -1; } /*! diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h index b27b718089..7c1ff94646 100644 --- a/src/widgets/itemviews/qheaderview_p.h +++ b/src/widgets/itemviews/qheaderview_p.h @@ -83,6 +83,7 @@ public: originalSize(-1), section(-1), target(-1), + firstPressed(-1), pressed(-1), hover(-1), length(0), @@ -277,6 +278,7 @@ public: int originalSize; int section; // used for resizing and moving sections int target; + int firstPressed; int pressed; int hover; diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index 610d99999a..8856a8ef6a 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -217,6 +217,7 @@ private slots: void QTBUG75615_sizeHintWithStylesheet(); void ensureNoIndexAtLength(); void offsetConsistent(); + void sectionsDontSortWhenNotClickingInThem(); void initialSortOrderRole(); @@ -2677,6 +2678,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) |