How can I get the first Item
/index
that is visible in a ListView
? I looked inside the documentation and also searched a lot on the Internet but couldn't find anything. Does anyone know how to do that?
Thank you!
How can I get the first Item
/index
that is visible in a ListView
? I looked inside the documentation and also searched a lot on the Internet but couldn't find anything. Does anyone know how to do that?
Thank you!
I know this is late but for others seeking help:
You can use the member method myView.indexAt(QPoint(0,0))
to find the first index.
I've also made a snippet to find all visible indexes in a view if you need that too: https://gist.github.com/iSplasher/8ebc42eaf9ea206b19bd
You should use something like that:
ListView {
id: contacts
model: UsersModel
onContentYChanged: {
var CurrentIndexAtTop = indexAt(1, contentY)
var CurrentPropFromModel = UsersModel.get(CurrentIndexAtTop).Name
}
}
if indexAt return -1 means not found, check this if need! contentY - it is a property of ListView, that return current position top Y-coord of ViewList window on flickable grid ListView.
see documentation for more details http://doc.qt.io/qt-5/qml-qtquick-listview.html#indexAt-method
Store the selected index when it changes. Once the model changes and the index becomes -1, you can use
positionViewAtIndex
to restore the right position.Here the documentation of the method.
Otherwise, you can do the same relying on the
add
andremove
method. Obviously, it works as far as the index of the selected item changes. You can also get the index of the visible item by means of theindexAt
method, but I've never used it before, even though it looks easy to use.So, you have several methods to get the index of the visible item and you can reset the view by means of the method above mentioned.