How To Use QML ListView: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=How to Use <span class="caps">QML</span> ListView=
[[Category:HowTo]]


==Introduction==
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


ListView provides a way to visualize contents of an one-dimensional model. Data can come from <span class="caps">QML</span> model elements like ListModel, XmlListModel, or C++ custom model classes inherited from QAbstractListModel.
= How to Use QML ListView =


ListView has a '''model''' property which supplies data to be visualized, and has a '''delegate''' property which supplies how ( <span class="caps">LAF</span> ) each row in the model is visualized. ListView is provided as a part of model/view paradigm in Qt/Qt Quick.
== Introduction ==


==ListView==
ListView provides a way to visualize contents of an one-dimensional model. Data can come from QML model elements like ListModel, XmlListModel, or C++ custom model classes inherited from QAbstractListModel.
 
ListView has a '''model''' property which supplies data to be visualized, and has a '''delegate''' property which supplies how ( LAF ) each row in the model is visualized. ListView is provided as a part of model/view paradigm in Qt/Qt Quick.
 
== ListView ==


ListView element needs to be told about the model to visualize and the delegate component to use to visualize each element of the model. This is done with '''model''' and '''delegate''' properties of ListView
ListView element needs to be told about the model to visualize and the delegate component to use to visualize each element of the model. This is done with '''model''' and '''delegate''' properties of ListView


===Example===
=== Example ===


Main.qml<br />
Main.qml<br /><code>import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> // create a model item instance<br /> SampleListModel {<br /> id: sampleModel<br /> }<br /> ListView {<br /> id: smapleListView<br /> anchors.fill: parent<br /> // concreate model<br /> model: sampleModel<br /> // provide delegate component.<br /> delegate: ExampleDelegate { }<br /> spacing: 4<br /> }<br />}</code>


Now let’s code ExampleDelegate component.
Now let's code ExampleDelegate component.


==Delegate==
== Delegate ==


Delegate components provide a way to visualize each row of model provided in ListView. An instance of delegate component is created dynamically for each row.
Delegate components provide a way to visualize each row of model provided in ListView. An instance of delegate component is created dynamically for each row.


===Example: Separate File with Delegate Component===
=== Example: Separate File with Delegate Component ===


ExampleDelegate.qml
ExampleDelegate.qml
<code>import QtQuick 1.0
Rectangle {<br /> id: delegateItem<br /> width: parent.width; height: 100<br /> color: &quot;blue&amp;quot;<br /> Image {<br /> id: imageItem<br /> height: parent.height; width: 100<br /> anchors.left: parent.left<br /> // deligate can directly ues ListElement role name<br /> source: imagePath<br /> }<br /> Text {<br /> id: itexItem<br /> anchors.left: imageItem.right<br /> anchors.leftMargin: 20<br /> anchors.verticalCenter: parent.verticalCenter<br /> font.pixelSize: 40<br /> // deligate can direclty use ListElement role name<br /> text: imageName<br /> }<br />}</code>


As you can see above, '''imagePath''', and '''imageName''' are not properties of delegateItem, so where are they coming from? They are coming from the model of ListView. '''The magic is that delegate components can access roles of each ListElement it visualizes directly inside it'''.
As you can see above, '''imagePath''', and '''imageName''' are not properties of delegateItem, so where are they coming from? They are coming from the model of ListView. '''The magic is that delegate components can access roles of each ListElement it visualizes directly inside it'''.
Line 29: Line 37:
Alternatively, we can place this delegate component in the same .qml file where we have ListView element.
Alternatively, we can place this delegate component in the same .qml file where we have ListView element.


===Example: Delegate Component Embedded in Main <span class="caps">QML</span> file===
=== Example: Delegate Component Embedded in Main QML file ===
 
<code>import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> Component {<br /> id: delegateComponent<br /> Item { ………. }<br /> }<br /> ListView {<br /> // provide delegate component.<br /> delegate: delegateComponent<br /> }<br />}</code>


or
or


Now, let’s create SimpleListModel model where each row has roles '''imagePath''', and '''imageName'''
<code>import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> ListView {<br /> // provide delegate component.<br /> delegate: Item {<br /> ……………<br /> }<br /> }<br />}</code>
 
Now, let's create SimpleListModel model where each row has roles '''imagePath''', and '''imageName'''


==Different ways of providing Model data==
== Different ways of providing Model data ==


As specified earlier, data for ListView can be provided with <span class="caps">QML</span> Model elements like ListModel, XmlListModel or C++ custom classes inherited from QAbstractListModel.
As specified earlier, data for ListView can be provided with QML Model elements like ListModel, XmlListModel or C++ custom classes inherited from QAbstractListModel.


===ListModel===
=== ListModel ===


ListModel provides a way to specify model data to ListView. Each row in ListModel is constructed with ListElement.
ListModel provides a way to specify model data to ListView. Each row in ListModel is constructed with ListElement.


Example: SampleListModel.qml<br />
Example:<br /> SampleListModel.qml<br /><code>import QtQuick 1.0<br />ListModel {<br /> ListElement { imagePath: &quot;1.jpg&amp;quot;; imageName: &quot;flower&amp;quot;}<br /> ListElement { imagePath: &quot;2.jpg&amp;quot;; imageName: &quot;house&amp;quot;}<br /> ListElement { imagePath: &quot;3.jpg&amp;quot;; imageName: &quot;water&amp;quot;}<br />}</code>


The above example defines a simple ListModel which can be used by a ListView. Each ListElement contains list of roles that can be directly accessed inside delegate element of ListView. <br /> Note: Copy three images with names “1.jpb”,“2.jpg”, and “3.jpg” to folder where you have qml files.
The above example defines a simple ListModel which can be used by a ListView. Each ListElement contains list of roles that can be directly accessed inside delegate element of ListView.<br />Note: Copy three images with names &quot;1.jpb&amp;quot;,&quot;2.jpg&amp;quot;, and &quot;3.jpg&amp;quot; to folder where you have qml files.


ListModel provides methods to manipulate ListModel from JavaScript. If we start with a empty ListModel, the first item inserted in to ListModel determines the roles that will be available to delegate in ListView.
ListModel provides methods to manipulate ListModel from JavaScript. If we start with a empty ListModel, the first item inserted in to ListModel determines the roles that will be available to delegate in ListView.


Example :<br />
Example :<br /><code>ListModel {<br /> id: sampleModel<br />}<br />…………..<br />sampleModel.append({&quot;name&amp;quot;:&quot;sonia&amp;quot;;&quot;age&amp;quot;:23})<br /></code>


Now, roles '''name''', and '''age''' will be available inside delegate of ListView. If we append more rows to above model with different roles, they will not be available to delegate.
Now, roles '''name''', and '''age''' will be available inside delegate of ListView. If we append more rows to above model with different roles, they will not be available to delegate.


Example :<br />
Example :<br /><code>ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />// insert a new row to model at index 1. Index starts from 0.<br />// This is equivalent to appending at the end as index we provide is 1.<br />sampleModel.insert(1,{&quot;name&amp;quot;:&quot;sonia&amp;quot;;&quot;age&amp;quot;:23})<br /></code>


In the above example, roles '''name''', and '''age''' will not be available in delegate. Only roles '''imagePath''', and '''imageName''' will be available.
In the above example, roles '''name''', and '''age''' will not be available in delegate. Only roles '''imagePath''', and '''imageName''' will be available.


To get row at a given index, use ListModel::get(index)<br /> Example :<br />
To get row at a given index, use ListModel::get(index)<br />Example :<br /><code>ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />// prints value of imagePath in the first row of sampleModel<br />console.log(sampleModel.get(0).imagePath)<br /></code>


To Modify values of roles of a particular row, use ListModel::set(int index,jsobject dict) method<br /> Example :<br />
To Modify values of roles of a particular row, use ListModel::set(int index,jsobject dict) method<br />Example :<br /><code>ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />sampleModel.set(0,{imagePath: &quot;image2.jpg&amp;quot;;imageName:&quot;water&amp;quot;}<br /></code>


To set value of a particular role in a given row, use ListModel::setProperty(int index, string rolename, variant value)<br /> Example :<br />
To set value of a particular role in a given row, use ListModel::setProperty(int index, string rolename, variant value)<br />Example :<br /><code>ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />sampleModel.setProperty(0,&quot;imagePath&amp;quot;,&quot;image2.jpg&amp;quot;}<br /></code>


To move elements inside ListModel use ListModel::move(int from, int to,int n)<br /> Moves n items from one position to another.
To move elements inside ListModel use ListModel::move(int from, int to,int n)<br />Moves n items from one position to another.


To remove a row from ListModel use ListModel::remove(index) and to clear all the rows in a model call ListModel.clear()
To remove a row from ListModel use ListModel::remove(index) and to clear all the rows in a model call ListModel.clear()
===Categories:===
* [[:Category:HowTo|HowTo]]

Revision as of 14:39, 23 February 2015


[toc align_right="yes&quot; depth="3&quot;]

How to Use QML ListView

Introduction

ListView provides a way to visualize contents of an one-dimensional model. Data can come from QML model elements like ListModel, XmlListModel, or C++ custom model classes inherited from QAbstractListModel.

ListView has a model property which supplies data to be visualized, and has a delegate property which supplies how ( LAF ) each row in the model is visualized. ListView is provided as a part of model/view paradigm in Qt/Qt Quick.

ListView

ListView element needs to be told about the model to visualize and the delegate component to use to visualize each element of the model. This is done with model and delegate properties of ListView

Example

Main.qml

import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> // create a model item instance<br /> SampleListModel {<br /> id: sampleModel<br /> }<br /> ListView {<br /> id: smapleListView<br /> anchors.fill: parent<br /> // concreate model<br /> model: sampleModel<br /> // provide delegate component.<br /> delegate: ExampleDelegate { }<br /> spacing: 4<br /> }<br />}

Now let's code ExampleDelegate component.

Delegate

Delegate components provide a way to visualize each row of model provided in ListView. An instance of delegate component is created dynamically for each row.

Example: Separate File with Delegate Component

ExampleDelegate.qml

import QtQuick 1.0

Rectangle {<br /> id: delegateItem<br /> width: parent.width; height: 100<br /> color: &quot;blue&amp;quot;<br /> Image {<br /> id: imageItem<br /> height: parent.height; width: 100<br /> anchors.left: parent.left<br /> // deligate can directly ues ListElement role name<br /> source: imagePath<br /> }<br /> Text {<br /> id: itexItem<br /> anchors.left: imageItem.right<br /> anchors.leftMargin: 20<br /> anchors.verticalCenter: parent.verticalCenter<br /> font.pixelSize: 40<br /> // deligate can direclty use ListElement role name<br /> text: imageName<br /> }<br />}

As you can see above, imagePath, and imageName are not properties of delegateItem, so where are they coming from? They are coming from the model of ListView. The magic is that delegate components can access roles of each ListElement it visualizes directly inside it.

Alternatively, we can place this delegate component in the same .qml file where we have ListView element.

Example: Delegate Component Embedded in Main QML file

import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> Component {<br /> id: delegateComponent<br /> Item { ………. }<br /> }<br /> ListView {<br /> // provide delegate component.<br /> delegate: delegateComponent<br /> }<br />}

or

import QtQuick 1.0<br />Item {<br /> id: rootItem<br /> ListView {<br /> // provide delegate component.<br /> delegate: Item {<br /> ……………<br /> }<br /> }<br />}

Now, let's create SimpleListModel model where each row has roles imagePath, and imageName

Different ways of providing Model data

As specified earlier, data for ListView can be provided with QML Model elements like ListModel, XmlListModel or C++ custom classes inherited from QAbstractListModel.

ListModel

ListModel provides a way to specify model data to ListView. Each row in ListModel is constructed with ListElement.

Example:
SampleListModel.qml

import QtQuick 1.0<br />ListModel {<br /> ListElement { imagePath: &quot;1.jpg&amp;quot;; imageName: &quot;flower&amp;quot;}<br /> ListElement { imagePath: &quot;2.jpg&amp;quot;; imageName: &quot;house&amp;quot;}<br /> ListElement { imagePath: &quot;3.jpg&amp;quot;; imageName: &quot;water&amp;quot;}<br />}

The above example defines a simple ListModel which can be used by a ListView. Each ListElement contains list of roles that can be directly accessed inside delegate element of ListView.
Note: Copy three images with names "1.jpb&quot;,"2.jpg&quot;, and "3.jpg&quot; to folder where you have qml files.

ListModel provides methods to manipulate ListModel from JavaScript. If we start with a empty ListModel, the first item inserted in to ListModel determines the roles that will be available to delegate in ListView.

Example :

ListModel {<br /> id: sampleModel<br />}<br />…………..<br />sampleModel.append({&quot;name&amp;quot;:&quot;sonia&amp;quot;;&quot;age&amp;quot;:23})<br />

Now, roles name, and age will be available inside delegate of ListView. If we append more rows to above model with different roles, they will not be available to delegate.

Example :

ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />// insert a new row to model at index 1. Index starts from 0.<br />// This is equivalent to appending at the end as index we provide is 1.<br />sampleModel.insert(1,{&quot;name&amp;quot;:&quot;sonia&amp;quot;;&quot;age&amp;quot;:23})<br />

In the above example, roles name, and age will not be available in delegate. Only roles imagePath, and imageName will be available.

To get row at a given index, use ListModel::get(index)
Example :

ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />// prints value of imagePath in the first row of sampleModel<br />console.log(sampleModel.get(0).imagePath)<br />

To Modify values of roles of a particular row, use ListModel::set(int index,jsobject dict) method
Example :

ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />sampleModel.set(0,{imagePath: &quot;image2.jpg&amp;quot;;imageName:&quot;water&amp;quot;}<br />

To set value of a particular role in a given row, use ListModel::setProperty(int index, string rolename, variant value)
Example :

ListModel {<br /> id: sampleModel<br /> ListElement { imagePath: &quot;image1.jpg&amp;quot;; imageName:&quot;house&amp;quot; }<br />}<br />…………..<br />sampleModel.setProperty(0,&quot;imagePath&amp;quot;,&quot;image2.jpg&amp;quot;}<br />

To move elements inside ListModel use ListModel::move(int from, int to,int n)
Moves n items from one position to another.

To remove a row from ListModel use ListModel::remove(index) and to clear all the rows in a model call ListModel.clear()