Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can i pass ListView's index to repeater delegate?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amir Afendin
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stcorp
      wrote on last edited by
      #2

      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}
                  }
              }
          }
      
      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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"}
        }

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        7

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved