For specifying a read-only model using XML data. 更多...
import 语句: | import QtQml.XmlListModel |
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:
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> ... <channel> <item> <title>Qt 6.0.2 Released</title> <link>https://www.qt.io/blog/qt-6.0.2-released</link> <pubDate>Wed, 03 Mar 2021 12:40:43 GMT</pubDate> </item> <item> <title>Qt 6.1 Beta Released</title> <link>https://www.qt.io/blog/qt-6.1-beta-released</link> <pubDate>Tue, 02 Mar 2021 13:05:47 GMT</pubDate> </item> <item> <title>Qt Creator 4.14.1 released</title> <link>https://www.qt.io/blog/qt-creator-4.14.1-released</link> <pubDate>Wed, 24 Feb 2021 13:53:21 GMT</pubDate> </item> </channel> </rss>
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>
在 XML 文档。
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>
。(见
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
被设为
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.
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.
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. |
另请参阅 progress .
errorString () |
Returns a string description of the last error that occurred if status is XmlListModel .Error.
reload () |
重新加载模型。