QML: Initialize empty list

喜夏-厌秋 提交于 2021-01-29 19:00:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!