Accessing a child/item's property in Repeater



  • 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@


  • 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.



  • 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.



  • 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.


Log in to reply
 

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