How can I get the first visible item/index from a ListView?
Asked Answered
S

4

6

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!

Sign answered 25/6, 2015 at 10:56 Comment(4)
You can probably exploit highlightRangeMode or the contentY properties. Anyhow, why do you want to know that? What's the specific result you want to achieve?Akanke
I want to hold the list view at the current position even if the model is changed. Now, if my model is changed the list jumps at the beginning.Sign
Oh, the problem from the other question. Maybe highlightRangeMode could be of help.Akanke
Ok, take a look at this example. The possible drawback of this approach is that the list moves strinctly within the highlight range which means that, when at the end, only the last Item is visible at the top of the list. See if the approach fits with your use case.Akanke
A
4

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

Andalusia answered 15/3, 2017 at 12:48 Comment(0)
Y
1

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

Yuille answered 26/12, 2015 at 13:24 Comment(1)
good to know, but it won't necessarily work with spacing > 0 and when scroll is per pixel and not per itemColorcast
C
0

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 and remove 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 the indexAt 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.

Condense answered 25/6, 2015 at 13:26 Comment(0)
C
0

Based on iSplasher's answer, the following works when QListView has spacing and/or has scroll by pixel:

sp = view.spacing()
first = max(view.indexAt(QPoint(sp, 0)), view.indexAt(QPoint(sp, sp * 2)))
Colorcast answered 31/1, 2019 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.