13

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.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82

2 Answers2

16

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.

Mitch
  • 23,716
  • 9
  • 83
  • 122
1

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

Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47