Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

  • Qt 4.8
  • XmlListModel QML Element

XmlListModel QML Element

TheXmlListModel element is used to specify a read-only model using XPath expressions.More...

Since: Qt 4.7

Properties

Methods

Detailed Description

XmlListModel is used to create a read-only model from XML data. It can be used as a data source for view elements (such asListView,PathView,GridView) and other elements that interact with model data (such asRepeater).

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 Sep201010:00:01 GMT</pubDate></item><item><title>Another blog post</title><pubDate>Sat,07 Sep201015:35:01 GMT</pubDate></item></channel></rss>

AXmlListModel could create a model from this data, like this:

import QtQuick 1.0XmlListModel {id:xmlModelsource:"http://www.mysite.com/feed.xml"query:"/rss/channel/item"XmlRole {name:"title";query:"title/string()" }XmlRole {name:"pubDate";query:"pubDate/string()" }}

Thequery value of "/rss/channel/item" specifies that theXmlListModel should generate a model item for each<item> in the XML document.

TheXmlRole objects define the model item attributes. Here, each model item will havetitle andpubDate attributes that match thetitle andpubDate values of its corresponding<item>. (SeeXmlRole::query for more examples of valid XPath expressions forXmlRole.)

The model could be used in aListView, like this:

ListView {width:180;height:300model:xmlModeldelegate:Text {text:title+": "+pubDate }}

TheXmlListModel data is loaded asynchronously, andstatus is set toXmlListModel.Ready when loading is complete. Note this means whenXmlListModel is used for a view, the view is not populated until the model is loaded.

Using key XML roles

You can define certain roles as "keys" so that whenreload() 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 whenreload() 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.

See alsoRSS News.

Property Documentation

count :int

The number of data entries in the model.


namespaceDeclarations :string

The namespace declarations to be used in the XPath queries.

The namespaces should be declared as inXQuery. 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()" }}

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 alsostatus andsource.


query :string

An absolute XPath query representing the base query for creating model items from this model'sXmlRole objects. The query should start with '/' or '//'.


roles :list<XmlRole>

The roles to make available for this model.


source :url

The location of the XML data source.

If bothsource andxml are set,xml is used.


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. SeeerrorString() for details about the error.

See alsoprogress.


xml :string

This property holds the XML data for this model, if set.

The text is assumed to be UTF-8 encoded.

If bothsource andxml are set,xml is used.


Method Documentation

voiderrorString()

Returns a string description of the last error that occurred ifstatus is XmlListModel::Error.


objectget(int index)

Returns the item atindex in the model.

For example, for a model like this:

XmlListModel {id:modelsource:"http://mysite.com/feed.xml"query:"/feed/entry"XmlRole {name:"title";query:"title/string()" }}

This will access thetitle value for the first item in the model:

vartitle =model.get(0).title;

reload()

Reloads the model.

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.

See alsoUsing key XML roles andXmlRole::isKey.


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp