Access Listview currentIndex from Delegate

前端 未结 2 1229
故里飘歌
故里飘歌 2021-02-03 15:07

I have a QML ListView where the delegate loads it\'s component from another file. When clicking on an delegate item, I want to update ListView. C

2条回答
  •  死守一世寂寞
    2021-02-03 15:35

    There are two problems here:

    • You need to qualify ListView's attached properties with the name of the item from which they're accessed.
    • The currentIndex property is a property of the ListView item type, not the attached property object.

    To fix them both, first change this:

    ListView.currentIndex = index;
    

    to this:

    delegate.ListView.view.currentIndex = index;
    

    And then give your delegate an id:

    Component {
        id: contact
    
        Rectangle {
            id: delegate
        // ...
    }
    

    This is demonstrated (in part) by the Example Usage section of the documentation:

    ListView attaches a number of properties to the root item of the delegate, for example ListView:isCurrentItem. In the following example, the root delegate item can access this attached property directly as ListView.isCurrentItem, while the child contactInfo object must refer to this property as wrapper.ListView.isCurrentItem.

提交回复
热议问题