XmlRole QML 类型

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

import 语句: import QtQuick.XmlListModel 2.12

特性

详细描述

另请参阅 Qt QML.

特性文档编制

isKey : bool

Defines whether this is a key role. Key roles are used to determine whether a set of values should be updated or added to the XML list model when XmlListModel::reload() 被调用。

另请参阅 XmlListModel .

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
    // ...
    XmlRole {
        name: "title"
        query: "title/string()"
    }
}
							
ListView {
    model: xmlModel
    delegate: Text { text: title }
}
							

query : string

The relative XPath expression query for this role. The query must be relative; it cannot start with a '/'.

例如,若有像这样的 XML 文档:


								

Here are some valid XPath expressions for XmlRole queries on this document:

XmlListModel {
    id: model
    ...
    // XmlRole queries will be made on <book> elements
    query: "/catalog/book"
    // query the book title
    XmlRole { name: "title"; query: "title/string()" }
    // query the book's year
    XmlRole { name: "year"; query: "year/number()" }
    // query the book's type (the '@' indicates 'type' is an attribute, not an element)
    XmlRole { name: "type"; query: "@type/string()" }
    // query the book's first listed author (note in XPath the first index is 1, not 0)
    XmlRole { name: "first_author"; query: "author[1]/string()" }
    // query the wanted attribute as a boolean
    XmlRole { name: "wanted"; query: "boolean(@wanted)" }
}
								

Accessing the model data for the above roles from a delegate:

ListView {
    width: 300; height: 200
    model: model
    delegate: Column {
        Text { text: title + " (" + type + ")"; font.bold: wanted }
        Text { text: first_author }
        Text { text: year }
    }
								

W3C XPath 2.0 规范 了解更多信息。