Details
-
Bug
-
Resolution: Duplicate
-
P2: Important
-
4.7.2
-
None
Description
To reproduce, in the following example try to click on items before and after scrolling the view. After the view has been scrolled, indexAt() will not return the correct result
import QtQuick 1.0 GridView { id: view width: 100 height: 100 cellHeight: 50 cellWidth: 50 clip: true model: ListModel { ListElement { name: 1 } ListElement { name: 2 } ListElement { name: 3 } ListElement { name: 4 } ListElement { name: 5 } ListElement { name: 6 } ListElement { name: 7 } ListElement { name: 8 } ListElement { name: 9 } ListElement { name: 0 } } delegate: Rectangle { width: 30 height: 30 color: "black" } MouseArea { anchors.fill: parent onClicked: { var index = view.indexAt(mouseX, mouseY); console.log("index: "+index+" data:"+model.get(index).name); } } }
The following patch fixes the problem:
--- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -2297,9 +2297,11 @@ void QDeclarativeGridView::positionViewAtEnd() int QDeclarativeGridView::indexAt(int x, int y) const { Q_D(const QDeclarativeGridView); + int scrolledX = x + contentX(); + int scrolledY = y + contentY(); for (int i = 0; i < d->visibleItems.count(); ++i) { const FxGridItem *listItem = d->visibleItems.at(i); - if(listItem->contains(x, y)) + if(listItem->contains(scrolledX, scrolledY)) return listItem->index; }
Attachments
Issue Links
- relates to
-
QTBUG-16672 ListView.indexAt() and GridView.indexAt() do not return the right item if the view has been scrolled
-
- Closed
-