summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews/qheaderview.cpp
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-11-25 22:03:38 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-12-01 15:57:10 +0100
commitb5b2640a65de20d05890fa9e6462bb7b88f83964 (patch)
tree352aa77a7d35f83d4f6adee7e329dd9f7530df71 /src/widgets/itemviews/qheaderview.cpp
parent10b6a87679d02d7cf5dcd2f75d5640ffb1e1303e (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. Pick-to: 6.0 5.15 Change-Id: Icfb67662221efbde019711f933781ee1e7d9ac43 Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/itemviews/qheaderview.cpp')
-rw-r--r--src/widgets/itemviews/qheaderview.cpp30
1 files changed, 24 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;
}
/*!