derM
derM

Reputation: 13701

Use the size of a model in QML

I am looking for a way to access the number of elements that are to be found in a model in QML.

For example:

Item {
    id: root
    Row {
        id: row
        height: root.height
        width: root.width
        spacing: 5
        Repeater {
            id: rep
            width: root.width
            height: root.height
            model: [5, 3, 3, 1, 12]
            delegate: myDelegate
            Text {
                id: myDelegate
                text: "Element " + index + " of " size + ":" + modelData
        }
    }
}

But I can't figure out how to retrieve the size of the model. In the documentation I can find a property called count, but no hint how to access it.

Upvotes: 13

Views: 17050

Answers (2)

Mariusz Jaskółka
Mariusz Jaskółka

Reputation: 4492

Generic way is to request repeater for it's underlying model count - in your case it would be rep.count.

Upvotes: 1

Mitch
Mitch

Reputation: 24416

It depends on the model you're using. In your case, the model is a plain old JavaScript array, so you'd use model.length. Every other Qt type related to models or views has a count property: ListModel, Repeater, ListView, etc. So, you could also use rep.count, in your case.

Upvotes: 18

Related Questions