IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

XmlListModelRole QML Type

For specifying a role to an XmlListModel.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

XmlListModelRole QML Type

  • Import Statement: import QtQml.XmlListModel

Detailed Description

 

See Also

See also Qt QML

Property Documentation

 

attributeName : string

The attribute of the XML element that will be used to read the data. The XML element is specified by elementName property.

For example, the following model has a role named "title", which reads the data from the XML element <title>. It also has another role named "timestamp", which uses the same XML element <title>, but reads its "created" attribute to extract the actual value.

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    XmlListModelRole { name: "title"; elementName: "title" }
    XmlListModelRole {
        name: "timestamp"
        elementName: "title"
        attributeName: "created"
    }
}

ListView {
    anchors.fill: parent
    model: xmlModel
    delegate: Text { text: title + " created on " + timestamp }
}

When the attributeName is specified, the elementName can be left empty. In this case the attribute of the top level XML element of the query will be read.

For example, if you have the following xml document:

 
Sélectionnez
&lt;documents&gt;
    &lt;document title="Title1"/&gt;
    &lt;document title="Title2"/&gt;
&lt;/documents&gt;

To extract the document titles you need the following model:

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    XmlListModelRole {
        name: "title"
        elementName: ""
        attributeName: "title"
    }
}

If you do not need to parse any attributes for the specified XML element, simply leave this property blank.

See Also

See also elementName

elementName : string

The name of the XML element, or a path to the XML element, that will be used to read the data. The element must actually contain text.

Optionally the attributeName property can be specified to extract the data.

For example, the following model has a role named "title", which reads the data from the XML element <title>. It also has another role named "timestamp", which uses the same XML element <title>, but reads its "created" attribute to extract the actual value.

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    XmlListModelRole { name: "title"; elementName: "title" }
    XmlListModelRole {
        name: "timestamp"
        elementName: "title"
        attributeName: "created"
    }
}

ListView {
    anchors.fill: parent
    model: xmlModel
    delegate: Text { text: title + " created on " + timestamp }
}

When the attributeName is specified, the elementName can be left empty. In this case the attribute of the top level XML element of the query will be read.

For example, if you have the following xml document:

 
Sélectionnez
&lt;documents&gt;
    &lt;document title="Title1"/&gt;
    &lt;document title="Title2"/&gt;
&lt;/documents&gt;

To extract the document titles you need the following model:

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    XmlListModelRole {
        name: "title"
        elementName: ""
        attributeName: "title"
    }
}

The elementName property can actually contain a path to the nested xml element. All the elements in the path must be joined with the '/' character.

For example, if you have the following xml document:

 
Sélectionnez
&lt;documents&gt;
    &lt;document&gt;
        &lt;title&gt;Title1&lt;/title&gt;
        &lt;info&gt;
            &lt;num_pages&gt;10&lt;/num_pages&gt;
        &lt;/info&gt;
    &lt;/document&gt;
    &lt;document&gt;
        &lt;title&gt;Title2&lt;/title&gt;
        &lt;info&gt;
            &lt;num_pages&gt;20&lt;/num_pages&gt;
        &lt;/info&gt;
    &lt;/document&gt;
&lt;/documents&gt;

You can extract the number of pages with the following role:

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    // ...
    XmlListModelRole {
        name: "pages"
        elementName: "info/num_pages"
    }
}

The path to the element must not start or end with '/'.

See Also

See also attributeName

name : string

The name for the role. This name is used to access the model data for this role.

For example, the following model has a role named "title", which can be accessed from the view's delegate:

 
Sélectionnez
XmlListModel {
    id: xmlModel
    source: "file.xml"
    query: "/documents/document"
    XmlListModelRole { name: "title"; elementName: "title" }
}
 
Sélectionnez
ListView {
    model: xmlModel
    delegate: Text { text: title }
}

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+