XmlListModelRole QML Type
Para especificar un rol a un XmlListModel. Más...
| Import Statement: | import QtQml.XmlListModel |
Propiedades
- attributeName : string
- elementName : string
- name : string
Descripción detallada
Véase también Qt Qml.
Documentación de propiedades
attributeName : string
Atributo del elemento XML que se utilizará para leer los datos. El elemento XML se especifica mediante la propiedad elementName.
Por ejemplo, el siguiente modelo tiene un rol llamado "title", que lee los datos del elemento XML <title>. También tiene otro rol llamado "timestamp", que utiliza el mismo elemento XML <title>, pero lee su atributo "created" para extraer el valor actual.
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 }
}Cuando se especifica el atributoName, el elemento elementName puede dejarse vacío. En este caso, se leerá el atributo del elemento XML de nivel superior de la consulta.
Por ejemplo, si tiene el siguiente documento xml:
<documents> <document title="Title1"/> <document title="Title2"/> </documents>
Para extraer los títulos del documento necesita el siguiente modelo:
XmlListModel { id: xmlModel source: "file.xml" query: "/documents/document" XmlListModelRole { name: "title" elementName: "" attributeName: "title" } }
Si no necesita analizar ningún atributo del elemento XML especificado, simplemente deje esta propiedad en blanco.
Véase también elementName.
elementName : string
El nombre del elemento XML, o una ruta al elemento XML, que se utilizará para leer los datos. El elemento debe contener texto.
Opcionalmente se puede especificar la propiedad attributeName para extraer los datos.
Por ejemplo, el siguiente modelo tiene un rol llamado "título", que lee los datos del elemento XML <title>. También tiene otro rol llamado "timestamp", que utiliza el mismo elemento XML <title>, pero lee su atributo "created" para extraer el valor actual.
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 }
}Cuando se especifica attributeName, el elementName puede dejarse vacío. En este caso se leerá el atributo del elemento XML de nivel superior de la consulta.
Por ejemplo, si tiene el siguiente documento xml:
<documents> <document title="Title1"/> <document title="Title2"/> </documents>
Para extraer los títulos del documento se necesita el siguiente modelo:
XmlListModel { id: xmlModel source: "file.xml" query: "/documents/document" XmlListModelRole { name: "title" elementName: "" attributeName: "title" } }
La propiedad elementName puede contener en realidad una ruta al elemento xml anidado. Todos los elementos de la ruta deben unirse con el carácter '/'.
Por ejemplo, si tiene el siguiente documento 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>
Puede extraer el número de páginas con la siguiente función:
XmlListModel { id: xmlModel source: "file.xml" query: "/documents/document" // ... XmlListModelRole { name: "pages" elementName: "info/num_pages" } }
Nota: La ruta al elemento no debe comenzar ni terminar con '/'.
Véase también attributeName.
name : string
Nombre del rol. Este nombre se utiliza para acceder a los datos del modelo para este rol.
Por ejemplo, el siguiente modelo tiene un rol llamado "título", al que se puede acceder desde el delegado de la vista:
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.