list QML Basic Type
The list
type refers to a list of QML objects.
A list value can be accessed in a similar way to a JavaScript array:
- Values are assigned using the
[]
square bracket syntax with comma-separated values - The
length
property provides the number of items in the list - Values in the list are accessed using the
[index]
syntax
Values can be dynamically added to the list by using the push
method, as if it were a JavaScript Array
A list
can only store QML objects, and cannot contain any basic type values. (To store basic types within a list, use the var type instead.)
When integrating with C++, note that any QQmlListProperty value passed into QML from C++ is automatically converted into a list
value, and vice-versa.
Using the list Type
For example, the Item type has a states list-type property that can be assigned to and used as follows:
import QtQuick 2.0 Item { width: 100; height: 100 states: [ State { name: "activated" }, State { name: "deactivated" } ] Component.onCompleted: { console.log("Name of first state:", states[0].name) for (var i = 0; i < states.length; i++) console.log("state", i, states[i].name) } }
The defined State objects will be added to the states
list in the order in which they are defined.
If the list only contains one object, the square brackets may be omitted:
import QtQuick 2.0 Item { width: 100; height: 100 states: State { name: "activated" } }
Objects in a list can be replaced with the []
operator, just like entries of JavaScript arrays. You can also use push()
to append entries, or you can set the length
property of the list to truncate or extend it. You can not automatically extend the list by assigning to an index currently out of range, though. Furthermore, if you insert null
values into the list, those are converted to nullptr
entries in the underlying QQmlListProperty.
This basic type is provided by the QML language.
See also QML Basic Types.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.