Get index of the delegate currently displayed - QML ListView

前端 未结 4 1019
无人共我
无人共我 2021-02-20 03:03

I created a ListView, which displays a couple of pages of content defined by the user (plain text). The page displayed is a delegate. Only one page is visible at a time. I decid

4条回答
  •  孤独总比滥情好
    2021-02-20 03:16

    There are many ways to get the index of current item that is displayed in the screen. If you can get the x-y coordinate of current page, you can use indexAt method in ListView.

    And in each delegate, you can find the index using index role within the scope of the delegate. The index is like a role you declared in your model, and is automatically assigned by ListView. For example,

    ListView 
    {
        delegate: Column
        {
            property int indexOfThisDelegate: index
            //...
        }
    }
    

    The index role is introduced here:

    A special index role containing the index of the item in the model is also available to the delegate. Note this index is set to -1 if the item is removed from the model...

    Another way is to explicitly assign value to the currentItem property in ListView, so the view can scroll by itself. Here is an simple example in Qt documentation, which is similar to your application.

提交回复
热议问题