XmlListModelRole QML Type
用于为XmlListModel 指定角色。更多内容...
| Import Statement: | import QtQml.XmlListModel |
属性
- attributeName : string
- elementName : string
- name : string
详细描述
另请参阅 Qt Qml.
属性文档
attributeName : string
用于读取数据的 XML 元素的属性。该 XML 元素由elementName 属性指定。
例如,以下模型中有一个名为“title”的角色,它从 XML 元素<title> 读取数据。该模型还有另一个名为“timestamp”的角色,它使用相同的 XML 元素<title> ,但读取其“created”属性以提取实际值。
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 时,elementName 可以留空。在这种情况下,将读取查询中顶级 XML 元素的属性。
例如,如果你有以下 XML 文档:
<documents>
<document title="Title1"/>
<document title="Title2"/>
</documents>要提取文档标题,您需要使用以下模型:
XmlListModel {
id: xmlModel
source: "file.xml"
query: "/documents/document"
XmlListModelRole {
name: "title"
elementName: ""
attributeName: "title"
}
}如果不需要解析指定 XML 元素的任何属性,只需将此属性留空即可。
另请参阅 elementName 。
elementName : string
用于读取数据的 XML 元素的名称,或该 XML 元素的路径。该元素必须实际包含文本内容。
可选地,可以指定attributeName 属性来提取数据。
例如,以下模型有一个名为“title”的角色,它从 XML 元素<title> 中读取数据。该模型还有另一个名为“timestamp”的角色,它使用相同的 XML 元素<title> ,但读取其“created”属性以提取实际值。
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 时,elementName 可以留空。在这种情况下,将读取查询中顶级 XML 元素的属性。
例如,假设你有以下 XML 文档:
<documents>
<document title="Title1"/>
<document title="Title2"/>
</documents>要提取文档标题,需要使用以下模型:
XmlListModel {
id: xmlModel
source: "file.xml"
query: "/documents/document"
XmlListModelRole {
name: "title"
elementName: ""
attributeName: "title"
}
}elementName 属性实际上可以包含指向嵌套 XML 元素的路径。路径中的所有元素必须用'/' 字符连接起来。
例如,如果你有以下 XML 文档:
<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>你可以使用以下角色提取页数:
XmlListModel {
id: xmlModel
source: "file.xml"
query: "/documents/document"
// ...
XmlListModelRole {
name: "pages"
elementName: "info/num_pages"
}
}注意: 元素的路径不能以'/' 开头或结尾。
另请参阅 attributeName 。
name : string
角色的名称。该名称用于访问此角色的模型数据。
例如,以下模型中有一个名为“title”的角色,可通过视图的委托访问该角色:
XmlListModel {
id: xmlModel
source: "file.xml"
query: "/documents/document"
XmlListModelRole { name: "title"; elementName: "title" }
}ListView {
model: xmlModel
delegate: Text { text: title }
}© 2026 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 the GNU 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.