使用 XPath 表达式指定只读模型。 更多...
import 语句: | import QtQuick.XmlListModel 2.12 |
To use this element, you will need to import the module with the following line:
import QtQuick.XmlListModel 2.0
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 ).
For example, if there is a XML document at http://www.mysite.com/feed.xml like this:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> ... <channel> <item> <title>A blog post</title> <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate> </item> <item> <title>Another blog post</title> <pubDate>Sat, 07 Sep 2010 15:35:01 GMT</pubDate> </item> </channel> </rss>
A XmlListModel could create a model from this data, like this:
import QtQuick 2.0 import QtQuick.XmlListModel 2.0 XmlListModel { id: xmlModel source: "http://www.mysite.com/feed.xml" query: "/rss/channel/item" XmlRole { name: "title"; query: "title/string()" } XmlRole { name: "pubDate"; query: "pubDate/string()" } }
The
query
value of "/rss/channel/item" specifies that the
XmlListModel
should generate a model item for each
<item>
在 XML 文档。
The
XmlRole
objects define the model item attributes. Here, each model item will have
title
and
pubDate
attributes that match the
title
and
pubDate
values of its corresponding
<item>
。(见
XmlRole::query
for more examples of valid XPath expressions for
XmlRole
)。
模型可以用于 ListView ,像这样:
ListView { width: 180; height: 300 model: xmlModel delegate: Text { text: title + ": " + pubDate } }
[Missing image qml-xmllistmodel-example.png]
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.
You can define certain roles as "keys" so that when reload() is called, the model will only add and refresh data that contains new values for these keys.
For example, if above role for "pubDate" was defined like this instead:
XmlRole { name: "pubDate"; query: "pubDate/string()"; isKey: true }
Then when reload() is called, the model will only add and reload items with a "pubDate" value that is not already present in the model.
This is useful when displaying the contents of XML documents that are incrementally updated (such as RSS feeds) to avoid repainting the entire contents of a model in a view.
If multiple key roles are specified, the model only adds and reload items with a combined value of all key roles that is not already present in the model.
另请参阅 Qt Quick 演示 - RSS 新闻 .
The number of data entries in the model.
The namespace declarations to be used in the XPath queries.
The namespaces should be declared as in XQuery . For example, if a requested document at http://mysite.com/feed.xml uses the namespace "http://www.w3.org/2005/Atom", this can be declared as the default namespace:
XmlListModel { source: "http://mysite.com/feed.xml" query: "/feed/entry" namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom';" XmlRole { name: "title"; query: "title/string()" } }
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.
An absolute XPath query representing the base query for creating model items from this model's XmlRole objects. The query should start with '/' or '//'.
roles : list < XmlRole > |
The roles to make available for this model.
The location of the XML data source.
If both
source
and
xml
are set,
xml
被使用。
Specifies the model loading status, which can be one of the following:
另请参阅 progress .
This property holds the XML data for this model, if set.
The text is assumed to be UTF-8 encoded.
If both
source
and
xml
are set,
xml
被使用。
Returns a string description of the last error that occurred if status is XmlListModel::Error.
返回项在 index in the model.
For example, for a model like this:
XmlListModel { id: model source: "http://mysite.com/feed.xml" query: "/feed/entry" XmlRole { name: "title"; query: "title/string()" } }
This will access the
title
value for the first item in the model:
var title = model.get(0).title;
重新加载模型。
If no key roles have been specified, all existing model data is removed, and the model is rebuilt from scratch.
Otherwise, items are only added if the model does not already contain items with matching key role values.
另请参阅 Using key XML roles and XmlRole::isKey .