QML Basic Type: list

A list of objects.

A list type contains a list of objects. While not technically a basic type, QML supports lists of object types. When used from QML, the engine automatically appends each value to the list. Items in the list can be accessed by index using the usual listName[index] syntax.

For example, the Item class contains a list property named children that can be used like this:

  1.                 Item  {
  2.     children: [
  3.         Item  { id: child1 },
  4.         Rectangle  { id: child2; width: 200 },
  5.         Text  { id: child3 }
  6.     ]
  7.  
  8.     Component.onCompleted:  {
  9.         console.log("Width of child rectangle:", children[1].width)
  10.     }
  11. }

child1, child2 and child3 will be added to the children list in the order in which they appear.

List properties can be created as a variant type, or as a list<Type> type, where Type is the type of the object in the list:

  1.                 Item  {
  2.     property list<Rectangle> rects: [
  3.         Rectangle  { width: 100; height: 100},
  4.         Rectangle  { width: 200; height: 200}
  5.     ]
  6. }

A list property can only contain values that match (or are derived from) the specified Type.

While the rects property can be reassigned to a different list value (including an empty list), its individual values cannot be modified. See the variant type documentation for details.

See also QML Basic Types.

Notes provided by the Qt Community

No notes