ListElement Element Reference

The ListElement element defines a data item in a ListModel.

This element was introduced in Qt 4.7.

Detailed Description

The ListElement element defines a data item in a ListModel.

List elements are defined inside ListModel definitions, and represent items in a list that will be displayed using ListView or Repeater items.

List elements are defined like other QML elements except that they contain a collection of role definitions instead of properties. Using the same syntax as property definitions, roles both define how the data is accessed and include the data itself.

The names used for roles must begin with a lower-case letter and should be common to all elements in a given model. Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

Referencing Roles

The role names are used by delegates to obtain data from list elements. Each role name is accessible in the delegate's scope, and refers to the corresponding role in the current element. Where a role name would be ambiguous to use, it can be accessed via the model property (e.g., model.cost instead of cost).

Example Usage

The following model defines a series of list elements, each of which contain "name" and "cost" roles and their associated values.

  1.                 ListModel  {
  2.     id: fruitModel
  3.  
  4.     ListElement  {
  5.         name: "Apple"
  6.         cost: 2.45
  7.     }
  8.     ListElement  {
  9.         name: "Orange"
  10.         cost: 3.25
  11.     }
  12.     ListElement  {
  13.         name: "Banana"
  14.         cost: 1.95
  15.     }
  16. }

The delegate obtains the name and cost for each element by simply referring to name and cost:

  1.                 ListView  {
  2.     anchors.fill: parent
  3.     model: fruitModel
  4.     delegate: Row  {
  5.         Text  { text: "Fruit: " + name }
  6.         Text  { text: "Cost: $" + cost }
  7.     }
  8. }
Notes provided by the Qt Community

No notes