Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-17917

GridView.indexAt() does not return correct result if the GridView has been scrolled

XMLWordPrintable

      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;
           }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            martjone Martin Jones (closed Nokia identity) (Inactive)
            naevdal Sigrid Fjell Nævdal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes