问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/61658194/qml-initialize-empty-list