Lærne
Lærne

Reputation: 3142

How to declare items directly in a list in QML?

I have a Dummy object in QML:

import QtQuicks 2.7
Item { property int foo: 0 }

And another abject needs to create a list of instances of such an object. But how do I declare it ? Using straight QML declarations in the list does not work :

import QtQuicks 2.7

Item {
    property var fools: [
        Dummy {foo: 1},
        Dummy {foo: 2},
        Dummy {foo: 3}
    ]
}

I get the error Cannot assign multiple values to a singular property

How to properly declare a list of dummies ?

Upvotes: 2

Views: 1352

Answers (1)

derM
derM

Reputation: 13691

If you use a recent Qt version (tested with Qt 5.9, I don't know when it was introduced, but not in 5.7) you can use the type:

property list<Item> itemList: [
    Item {},
    Item {},
    ...
]

Upvotes: 5

Related Questions