Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the size of a model in QML

Tags:

qt

qml

qtquick2

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.

like image 407
derM Avatar asked Aug 02 '16 08:08

derM


Video Answer


2 Answers

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.

like image 129
Mitch Avatar answered Sep 22 '22 13:09

Mitch


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

like image 37
Mariusz Jaskółka Avatar answered Sep 19 '22 13:09

Mariusz Jaskółka