XmlListModel QML Type▲
-
Import Statement: import QtQml.XmlListModel
Detailed Description▲
To use this element, you will need to import the module with the following line:
import
QtQml.XmlListModel
XmlListModel is used to create a read-only model from XML data. It can be used as a data source for view elements (such as ListView, PathView, GridView) and other elements that interact with model data (such as Repeater).
This model does not support the XPath queries. It supports simple slash-separated paths and, optionally, one attribute for each element.
For example, if there is an XML document at https://www.qt.io/blog/rss.xml like this:
&
lt;?xml version=
"1.0"
encoding=
"UTF-8"
?&
gt;
&
lt;rss version=
"2.0"
&
gt;
...
&
lt;channel&
gt;
&
lt;item&
gt;
&
lt;title&
gt;Qt 6.0.2
Released&
lt;/
title&
gt;
&
lt;link&
gt;https://www.qt.io/blog/qt-6.0.2-released</link>
&
lt;pubDate&
gt;Wed, 03
Mar 2021
12
:40
:43
GMT&
lt;/
pubDate&
gt;
&
lt;/
item&
gt;
&
lt;item&
gt;
&
lt;title&
gt;Qt 6.1
Beta Released&
lt;/
title&
gt;
&
lt;link&
gt;https://www.qt.io/blog/qt-6.1-beta-released</link>
&
lt;pubDate&
gt;Tue, 02
Mar 2021
13
:05
:47
GMT&
lt;/
pubDate&
gt;
&
lt;/
item&
gt;
&
lt;item&
gt;
&
lt;title&
gt;Qt Creator 4.14.1
released&
lt;/
title&
gt;
&
lt;link&
gt;https://www.qt.io/blog/qt-creator-4.14.1-released</link>
&
lt;pubDate&
gt;Wed, 24
Feb 2021
13
:53
:21
GMT&
lt;/
pubDate&
gt;
&
lt;/
item&
gt;
&
lt;/
channel&
gt;
&
lt;/
rss&
gt;
A XmlListModel could create a model from this data, like this:
import
QtQml.XmlListModel
XmlListModel
{
id
:
xmlModel
source
:
"https://www.qt.io/blog/rss.xml"
query
:
"/rss/channel/item"
XmlListModelRole {
name
:
"title"
; elementName
:
"title"
}
XmlListModelRole {
name
:
"pubDate"
; elementName
:
"pubDate"
}
XmlListModelRole {
name
:
"link"
; elementName
:
"link"
}
}
The query value of "/rss/channel/item" specifies that the XmlListModel should generate a model item for each <item> in the XML document.
The XmlListModelRole objects define the model item attributes. Here, each model item will have title, pubDate and link attributes that match the title, pubDate and link values of its corresponding <item>. (See XmlListModelRole documentation for more examples.)
The model could be used in a ListView, like this:
ListView
{
width
:
180
; height
:
300
model
:
xmlModel
delegate
:
Text
{
text
:
title
+
": "
+
pubDate +
"; link: "
+
link }
}
The XmlListModel data is loaded asynchronously, and status is set to XmlListModel.Ready when loading is complete. Note this means when XmlListModel is used for a view, the view is not populated until the model is loaded.
Property Documentation▲
count : int▲
The number of data entries in the model.
progress : real▲
This indicates the current progress of the downloading of the XML data source. This value ranges from 0.0 (no data downloaded) to 1.0 (all data downloaded). If the XML data is not from a remote source, the progress becomes 1.0 as soon as the data is read.
Note that when the progress is 1.0, the XML data has been downloaded, but it is yet to be loaded into the model at this point. Use the status property to find out when the XML data has been read and loaded into the model.
See Also▲
query : string▲
A string representing the base path for creating model items from this model's XmlListModelRole objects. The query should start with '/'.
roles : list<XmlListModelRole>▲
The roles to make available for this model.
source : url▲
The location of the XML data source.
status : enumeration▲
Specifies the model loading status, which can be one of the following:
-
XmlListModel.Null - No XML data has been set for this model.
-
XmlListModel.Ready - The XML data has been loaded into the model.
-
XmlListModel.Loading - The model is in the process of reading and loading XML data.
-
XmlListModel.Error - An error occurred while the model was loading. See errorString() for details about the error.
See Also▲
See also progress
Method Documentation▲
errorString()▲
Returns a string description of the last error that occurred if status is XmlListModel.Error.
reload()▲
Reloads the model.