diff options
-rw-r--r-- | src/quick/items/qquicklistview.cpp | 7 | ||||
-rw-r--r-- | tests/auto/quick/qquicklistview/data/snapToItemWithSpacing.qml | 18 | ||||
-rw-r--r-- | tests/auto/quick/qquicklistview/tst_qquicklistview.cpp | 26 |
3 files changed, 50 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; diff --git a/tests/auto/quick/qquicklistview/data/snapToItemWithSpacing.qml b/tests/auto/quick/qquicklistview/data/snapToItemWithSpacing.qml new file mode 100644 index 0000000000..50b5abb206 --- /dev/null +++ b/tests/auto/quick/qquicklistview/data/snapToItemWithSpacing.qml @@ -0,0 +1,18 @@ +import QtQuick 2.0 + +ListView { + width: 100 + height: 300 + snapMode: ListView.SnapToItem + spacing: 100 + model: 10 + delegate: Rectangle { + height: 100 + width: 100 + color: "blue" + Text { + anchors.centerIn: parent + text: index + } + } +} diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index 8bc5575e34..4d3a665255 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -176,6 +176,7 @@ private slots: void creationContext(); void snapToItem_data(); void snapToItem(); + void snapToItemWithSpacing_QTBUG_59852(); void snapOneItemResize_QTBUG_43555(); void snapOneItem_data(); void snapOneItem(); @@ -5197,6 +5198,31 @@ void tst_QQuickListView::snapToItem() releaseView(window); } +void tst_QQuickListView::snapToItemWithSpacing_QTBUG_59852() +{ + QQuickView *window = getView(); + + window->setSource(testFileUrl("snapToItemWithSpacing.qml")); + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + auto *listView = qobject_cast<QQuickListView*>(window->rootObject()); + QVERIFY(listView); + + QTRY_COMPARE(QQuickItemPrivate::get(listView)->polishScheduled, false); + + // each item in the list is 100 pixels tall, and the spacing is 100 + + listView->setContentY(110); // this is right below the first item + listView->returnToBounds(); + QCOMPARE(listView->contentY(), 200); // the position of the second item + + listView->setContentY(60); // this is right below the middle of the first item + listView->returnToBounds(); + QCOMPARE(listView->contentY(), 0); // it's farther to go to the next item, so snaps to the first + + releaseView(window); +} void tst_QQuickListView::snapOneItemResize_QTBUG_43555() { |