C

ListModel QML Type

Defines a free-form list data source. More...

Import Statement: import QtQuick
Instantiates: ListModel

Properties

Methods

  • var data(int index)
  • var get(int index)

Detailed Description

The ListModel is a simple container of ListElement definitions, each containing data roles. The contents can be defined declaratively in QML.

As a Qul::ListModel, the the number of elements in the model can be obtained from its count property and data can be accessed with data or get.

Contrary to Qt Quick, ListModels are read-only in Qt Quick Ultralite. There are no functions for adding, removing or changing elements. See Qt Quick Ultralite differences in models.

Example Usage

The following example shows a ListModel containing three elements, with the roles "name" and "cost".

ListModel {
    id: fruitModel

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

Roles (properties) in each element must begin with a lower-case letter and should be common to all elements in a model. The ListElement documentation provides more guidelines for how elements should be defined.

The example model contains an id property, which can be referenced by views, such as the ListView in this example:

import QtQuick 2.15

Rectangle {
    width: 200; height: 200

    ListModel {
        id: fruitModel
        ...
    }

    Component {
        id: fruitDelegate
        Row {
            width: view.width / fruitModel.count
            height: view.height / fruitModel.count
            spacing: 10
            Text { text: model.name }
            Text { text: '$' + model.cost }
        }
    }

    ListView {
        id: view
        anchors.fill: parent
        model: fruitModel
        delegate: fruitDelegate
    }
}

See also ListElement and Qt Quick Ultralite differences in models.

Property Documentation

count : int

The number of data entries in the model.


Method Documentation

var data(int index)

Access model data at index.


var get(int index)

Access model data at index.


Available under certain Qt licenses.
Find out more.