ListElement QML Type

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

Import Statement: import QtQuick .

Detailed Description

List elements are defined inside ListModel definitions, and represent items in a list that is 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 define how to access the data and include the data itself.

The names used for roles must begin with a lower-case letter and must be common to all elements in a given model. Values must be simple constants; either strings (optionally wrapped in qsTr), 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 via the modelData property and refers to the corresponding role in the current element. For example, modelData.cost refers to the current data for the cost role.

Note: Qt Quick Ultralite does not allow unqualified access to model roles. Additionally, Qt Quick uses model for role access while Qt Quick Ultralite uses modelData. See Qt Quick Ultralite differences in models.

Example Usage

The following model defines a series of list elements, each of which contain "name" and "cost" roles along with 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 ListElement by referring to modelData.name and modelData.cost:

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

See also ListModel and Qt Quick Ultralite differences in models.

Available under certain Qt licenses.
Find out more.