diff options
author | Johan Klokkhammer Helsing <johan.helsing@qt.io> | 2018-04-23 13:13:31 +0200 |
---|---|---|
committer | Johan Helsing <johan.helsing@qt.io> | 2018-04-25 04:28:48 +0000 |
commit | 281c24d9169396a62f409de6dce3545f8d48ca8b (patch) | |
tree | 7581ecfab4914dfc57d68a4a22850a5540a7c270 /src/quick/items/qquicklistview.cpp | |
parent | ac6e893c5c7b43cfc50c25bebee1dfd4d3840124 (diff) |
Fix QQuickListViewPrivate::snapItemAt for lists with spacing
If lists had spacing, snapping would have gaps where snapItemAt would
return nullptr eventhough there were items above and below in the list.
I.e. if we have a list with items of size 100, and a spacing of 100, and
scroll to 110 (right below the first item), then the first item would fail
because this is false:
itemTop+itemSize()/2 >= pos (50 >= 110)
And the next item (the one we should have snapped to), would fail because
this is false:
itemTop-prevItemSize/2 < pos (200-100/2 < 110)
The meaning of first part was probably to see if the middle of the current
item is inside the view.
The meaning of the second part was probably to see if the middle of the
previous item is outside the view.
This patch refactors the code so it's more visible what's happening and
fixes the bug by taking the spacing into account when computing the position of
the halfway positions.
Task-number: QTBUG-59852
Change-Id: I60eec0d16e91d2f22d70b97d11bcde5d88ac2997
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick/items/qquicklistview.cpp')
-rw-r--r-- | src/quick/items/qquicklistview.cpp | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/quick/items/qquicklistview.cpp b/src/quick/items/qquicklistview.cpp index 33becd71ec..60f7efae12 100644 --- a/src/quick/items/qquicklistview.cpp +++ b/src/quick/items/qquicklistview.cpp @@ -538,8 +538,13 @@ FxViewItem *QQuickListViewPrivate::snapItemAt(qreal pos) qreal itemTop = item->position(); if (highlight && itemTop >= pos && item->endPosition() <= pos + highlight->size()) return item; - if (itemTop+item->size()/2 >= pos && itemTop-prevItemSize/2 < pos) + + // Middle of item and spacing (i.e. the middle of the distance between this item and the next + qreal halfwayToNextItem = itemTop + (item->size()+spacing) / 2; + qreal halfwayToPrevItem = itemTop - (prevItemSize+spacing) / 2; + if (halfwayToNextItem >= pos && halfwayToPrevItem < pos) snapItem = item; + prevItemSize = item->size(); } return snapItem; |