XmlListModelRole QML Type

指定角色为 XmlListModel . 更多...

import 语句: import QtQml.XmlListModel

特性

详细描述

另请参阅 Qt QML .

特性文档编制

attributeName : string

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

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.

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:

<documents>
    <document title="Title1"/>
    <document title="Title2"/>
</documents>
						

To extract the document titles you need the following model:

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.

另请参阅 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.

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 }
}
							

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:

<documents>
    <document title="Title1"/>
    <document title="Title2"/>
</documents>
							

To extract the document titles you need the following model:

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:

<documents>
    <document>
        <title>Title1</title>
        <info>
            <num_pages>10</num_pages>
        </info>
    </document>
    <document>
        <title>Title2</title>
        <info>
            <num_pages>20</num_pages>
        </info>
    </document>
</documents>
							

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

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 '/' .

另请参阅 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:

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