ListModel Element Reference

The ListModel element defines a free-form list data source.

This element was introduced in Qt 4.7.

Detailed Description

The ListModel element defines a free-form list data source.

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

The number of elements in the model can be obtained from its count property. A number of familiar methods are also provided to manipulate the contents of the model, including append(), insert(), move(), remove() and set(). These methods accept dictionaries as their arguments; these are translated to ListElement objects by the model.

Elements can be manipulated via the model using the setProperty() method, which allows the roles of the specified element to be set and changed.

Example Usage

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

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

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.

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

  1. import QtQuick 1.0
  2.  
  3. Rectangle  {
  4.     width: 200; height: 200
  5.  
  6.     ListModel  {
  7.         id: fruitModel
  8.         ...
  9.     }
  10.  
  11.     Component  {
  12.         id: fruitDelegate
  13.         Row  {
  14.             spacing: 10
  15.             Text  { text: name }
  16.             Text  { text: '$' + cost }
  17.         }
  18.     }
  19.  
  20.     ListView  {
  21.         anchors.fill: parent
  22.         model: fruitModel
  23.         delegate: fruitDelegate
  24.     }
  25. }

It is possible for roles to contain list data. In the following example we create a list of fruit attributes:

  1. ListModel  {
  2.     id: fruitModel
  3.  
  4.     ListElement  {
  5.         name: "Apple"
  6.         cost: 2.45
  7.         attributes: [
  8.             ListElement  { description: "Core" },
  9.             ListElement  { description: "Deciduous" }
  10.         ]
  11.     }
  12.     ListElement  {
  13.         name: "Orange"
  14.         cost: 3.25
  15.         attributes: [
  16.             ListElement  { description: "Citrus" }
  17.         ]
  18.     }
  19.     ListElement  {
  20.         name: "Banana"
  21.         cost: 1.95
  22.         attributes: [
  23.             ListElement  { description: "Tropical" },
  24.             ListElement  { description: "Seedless" }
  25.         ]
  26.     }
  27. }

The delegate displays all the fruit attributes:

  1.                 Component  {
  2.     id: fruitDelegate
  3.     Item  {
  4.         width: 200; height: 50
  5.         Text  { id: nameField; text: name }
  6.         Text  { text: '$' + cost; anchors.left: nameField.right }
  7.         Row  {
  8.             anchors.top: nameField.bottom
  9.             spacing: 5
  10.             Text  { text: "Attributes:" }
  11.             Repeater  {
  12.                 model: attributes
  13.                 Text  { text: description }
  14.             }
  15.         }
  16.     }
  17. }

Modifying List Models

The content of a ListModel may be created and modified using the clear(), append(), set(), insert() and setProperty() methods. For example:

  1.     Component  {
  2.         id: fruitDelegate
  3.         Item  {
  4.             width: 200; height: 50
  5.             Text  { text: name }
  6.             Text  { text: '$' + cost; anchors.right: parent.right }
  7.  
  8.             // Double the price when clicked.
  9.             MouseArea  {
  10.                 anchors.fill: parent
  11.                 onClicked: fruitModel.setProperty(index, "cost", cost * 2)
  12.             }
  13.         }
  14.     }

Note that when creating content dynamically the set of available properties cannot be changed once set. Whatever properties are first added to the model are the only permitted properties in the model.

Using Threaded List Models with WorkerScript

ListModel can be used together with WorkerScript access a list model from multiple threads. This is useful if list modifications are synchronous and take some time: the list operations can be moved to a different thread to avoid blocking of the main GUI thread.

Here is an example that uses WorkerScript to periodically append the current time to a list model:

  1. import QtQuick 1.0
  2.  
  3. Rectangle  {
  4.     color: "white"
  5.     width: 200
  6.     height: 300
  7.  
  8.     ListView  {
  9.         anchors.fill: parent
  10.         model: listModel
  11.         delegate: Component  {
  12.             Text  { text: time }
  13.         }
  14.  
  15.         ListModel  { id: listModel }
  16.  
  17.         WorkerScript  {
  18.             id: worker
  19.             source: "dataloader.js"
  20.         }
  21.  
  22.         Timer  {
  23.             id: timer
  24.             interval: 2000; repeat: true
  25.             running: true
  26.             triggeredOnStart: true
  27.  
  28.             onTriggered:  {
  29.                 var msg =  {'action': 'appendCurrentTime', 'model': listModel};
  30.                 worker.sendMessage(msg);
  31.             }
  32.         }
  33.     }
  34. }

The included file, dataloader.js, looks like this:

The timer in the main example sends messages to the worker script by calling WorkerScript::sendMessage(). When this message is received, WorkerScript.onMessage() is invoked in dataloader.js, which appends the current time to the list model.

Note the call to sync() from the WorkerScript.onMessage() handler. You must call sync() or else the changes made to the list from the external thread will not be reflected in the list model in the main thread.

Restrictions

If a list model is to be accessed from a WorkerScript, it cannot contain list-type data. So, the following model cannot be used from a WorkerScript because of the list contained in the "attributes" property:

  1. ListModel  {
  2.     id: fruitModel
  3.     ListElement  {
  4.         name: "Apple"
  5.         cost: 2.45
  6.         attributes: [
  7.             ListElement  { description: "Core" },
  8.             ListElement  { description: "Deciduous" }
  9.         ]
  10.     }
  11. }

In addition, the WorkerScript cannot add list-type data to the model.

Properties

Methods

Property Documentation

The number of data entries in the model.

Method Documentation

  • ListModel::append ( jsobject dict )

Adds a new item to the end of the list model, with the values in dict.

  1. fruitModel.append( {"cost": 5.95, "name":"Pizza"})

See also set() and remove().

  • ListModel::clear ()

Deletes all content from the model.

See also append() and remove().

  • object ListModel::get ( int index )

Returns the item at index in the list model. This allows the item data to be accessed or modified from JavaScript:

  1. Component.onCompleted:  {
  2.     fruitModel.append( {"cost": 5.95, "name":"Jackfruit"});
  3.     console.log(fruitModel.get(0).cost);
  4.     fruitModel.get(0).cost = 10.95;
  5. }

The index must be an element in the list.

Note that properties of the returned object that are themselves objects will also be models, and this get() method is used to access elements:

  1.     fruitModel.append(..., "attributes":
  2.         [ {"name":"spikes","value":"7mm"},
  3.           {"name":"color","value":"green"}]);
  4.     fruitModel.get(0).attributes.get(1).value; // == "green"

Warning: The returned object is not guaranteed to remain valid. It should not be used in property bindings.

See also append().

  • ListModel::insert ( int index, jsobject dict )

Adds a new item to the list model at position index, with the values in dict.

  1. fruitModel.insert(2,  {"cost": 5.95, "name":"Pizza"})

The index must be to an existing item in the list, or one past the end of the list (equivalent to append).

See also set() and append().

  • ListModel::move ( int from, int to, int n )

Moves n items from one position to another.

The from and to ranges must exist; for example, to move the first 3 items to the end of the list:

  1. fruitModel.move(0, fruitModel.count - 3, 3)

See also append().

  • ListModel::remove ( int index )

Deletes the content at index from the model.

See also clear().

  • ListModel::set ( int index, jsobject dict )

Changes the item at index in the list model with the values in dict. Properties not appearing in dict are left unchanged.

  1. fruitModel.set(3,  {"cost": 5.95, "name":"Pizza"})

If index is equal to count() then a new item is appended to the list. Otherwise, index must be an element in the list.

See also append().

Changes the property of the item at index in the list model to value.

  1. fruitModel.setProperty(3, "cost", 5.95)

The index must be an element in the list.

See also append().

  • ListModel::sync ()

Writes any unsaved changes to the list model after it has been modified from a worker script.

Notes provided by the Qt Community

No notes