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. Accessing a child/item's property in Repeater

Accessing a child/item's property in Repeater

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.6k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kwisatz
    wrote on last edited by
    #1

    How do I access the text property of xText for various children/items (a,b,c)?

    I'm able to initialize the property using the model, but what if I want to modify it after initialization or bind to it in C++?

    @

    property string a: "A"
    property string b: "B"
    property string c: "C"

    Row {
    id: xRow
    anchors.centerIn: parent
    visible: true
    spacing: 5

            Repeater {
                id:xRepeater
                model: [a, b, c]
    
                Image {
                    id:xImage
                    width: 78; height: 70
                    fillMode: Image.Stretch
                    source: "imgs/x.png"
                    smooth: true
    
                    Text {
                        id: xText
                        anchors {centerIn: parent}
                        text: modelData
                        font {pixelSize: 36}
                        color: "black"
                    }
    
                }//end of image
    
            }//end of repeater block
    
        }//end of of row block@
    
    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on last edited by
      #2

      Hi,
      For this you will need a dynamic model like ListModel or if you want to access from C++ a child class of QAbstractItemModel or a QList<QObject*>. I would recommend using QAbstractItemModel. With this kind of model, changes to the model will cause the repeater to update its content.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kwisatz
        wrote on last edited by
        #3

        Thanks, I was thinking something along this lines. But was hoping to uses the qml model as is and just bind to property value by finding the object through the object tree.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on last edited by
          #4

          Hi,
          You can try something like this:
          @
          property list<QtObject> myModel: [
          QtObject{text: "A"},
          QtObject{text: "B"},
          QtObject{text: "C"}
          ]

          Row {
          id: xRow
          anchors.centerIn: parent
          visible: true
          spacing: 5

                  Repeater {
                      id:xRepeater
                      model: myModel
          
                      Image {
                          id:xImage
                          width: 78; height: 70
                          fillMode: Image.Stretch
                          source: "imgs/x.png"
                          smooth: true
          
                          Text {
                              id: xText
                              anchors {centerIn: parent}
                              text: modelData.text
                              font {pixelSize: 36}
                              color: "black"
                          }
          
                      }//end of image
          
                  }//end of repeater block
          
              }//end of of row block
          

          @
          This way, changes in your object properties should cause binding reevaluation. However, I don't know how this would be mapped into C++. Probably a QList<QObject*> or a QVariantList but you will have to test this to be sure.

          1 Reply Last reply
          0

          • Login

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