Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QML XmlListModel Element

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

This element was introduced in 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 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 1.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> in the XML document.

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>. (See XmlRole::query for more examples of valid XPath expressions for XmlRole.)

The model could be used in a ListView, like this:

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

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.

Using key XML roles

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.

See also RSS News.

Property Documentation

read-onlycount : 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 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()" }
 }

read-onlyprogress : 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 status and source.


query : string

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


read-onlyroles : list<XmlRole>

The roles to make available for this model.


source : url

The location of the XML data source.

If both source and xml are set, xml is used.


read-onlystatus : 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 progress.


xml : string

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 is used.


Method Documentation

void XmlListModel::errorString ()

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


object XmlListModel::get ( int index )

Returns the item at 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;

XmlListModel::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 also Using key XML roles and XmlRole::isKey.


Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Les composants

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web