Defines a data item in a ListModel. More...

Import Statement: import QtQml.Models 2.11

Detailed Description

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).

Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions.

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.

ListModel {
    id: fruitModel

    ListElement {
        name: "Apple"
        cost: 2.45
    ListElement {
        name: "Orange"
        cost: 3.25
    ListElement {
        name: "Banana"
        cost: 1.95

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

ListView {
    anchors.fill: parent
    model: fruitModel
    delegate: Row {
        Text { text: "Fruit: " + name }
        Text { text: "Cost: $" + cost }

See also ListModel.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support