How can i pass ListView's index to repeater delegate?



  • Hello, i have situation like this

    ListView {
        delegate: Rectangle {
            Repeater {
                model: 6
                delegate: Text { text: "repeater index: " + index + " listview index: " + /*listview's index*/}
        }
    }
    

    how can i solve this? just tried a lot of things and still didn't get it.



  • Something like this should work:

    ListView {
            delegate: Rectangle {
                property var listviewIndex: model.index
                Repeater {
                    model: 6
                    delegate: Text { text: "repeater index: " + index + " listview index: " + listviewIndex}
                }
            }
        }
    


  • Also index property is exposed to delegate. Assign the value by defining new property inside the delegate. Just see the code below.

    Not sure what you are trying to do with Repeater. Repeater is just object creation. With you code everything overlaps. You need to put them in some positioners like Column/Row.

    ===main.qml=====
    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListView {
        id : view
        anchors.fill : parent
        spacing: 5
        model : MyModel {}
        delegate: Rectangle {
            id : del
            width:  200;height: 100;color : "yellow"
            property int myindex : index
            Column {
                Repeater {
                    model: 6
                    delegate: Text { text: "repeater index: " + index + " View Index="+myindex}
                }
            }
        }
    }
    

    }

    ===MyModel.qml======
    import QtQuick 2.0

    ListModel{
    ListElement { name :"pthinks.com"}
    ListElement { name :"pthinks.com"}
    ListElement { name :"pthinks.com"}
    ListElement { name :"pthinks.com"}
    ListElement { name :"pthinks.com"}
    ListElement { name :"pthinks.com"}
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.