skaldesh
skaldesh

Reputation: 1530

QML: Initialize empty list

I want to do the following:

property list<MenuItem> menuItems: []

This fails with:

Unexpected token `]'

However, this works:

property list<MenuItem> menuItems: [MenuItem{}]

So how do I initialize an empty list here?

Upvotes: 2

Views: 1778

Answers (1)

David K. Hess
David K. Hess

Reputation: 17246

Like this:

property list<MenuItem> menuItems

The declaration with [] requires at least one item.

A list is not as flexible as a Javascript Array:

Note that objects cannot be individually added to or removed from the list once created; to modify the contents of a list, it must be reassigned to a new list.

Though you can append to it:

Values can be dynamically added to the list by using the push method, as if it were a JavaScript Array

More info here: https://doc.qt.io/qt-5/qml-list.html

Upvotes: 2

Related Questions