Tutorial de Ordenación Dinámica de Vistas QML 1 - Un ListView Simple y un Delegado
Comenzamos nuestra aplicación definiendo un ListView, un modelo que proporcionará datos a la vista, y un delegado que proporciona una plantilla para construir elementos en la vista.
El código para ListView y el delegado tiene el siguiente aspecto:
import QtQuick Rectangle { id: root width: 300 height: 400 Component { id: dragDelegate Rectangle { id: content required property string name required property string type required property string size required property int age width: view.width height: column.implicitHeight + 4 border.width: 1 border.color: "lightsteelblue" radius: 2 Column { id: column anchors { fill: parent margins: 2 } Text { text: qsTr('Name: ') + content.name } Text { text: qsTr('Type: ') + content.type } Text { text: qsTr('Age: ') + content.age } Text { text: qsTr('Size: ') + content.size } } } } ListView { id: view anchors { fill: parent margins: 2 } model: PetsModel {} delegate: dragDelegate spacing: 4 cacheBuffer: 50 } }
El modelo se define en un archivo QML separado que tiene este aspecto:
import QtQuick ListModel { ListElement { name: qsTr("Polly") type: qsTr("Parrot") age: 12 size: qsTr("Small") } ListElement { name: qsTr("Penny") type: qsTr("Turtle") age: 4 size: qsTr("Small") } }
Recorrido
El primer elemento definido dentro del Rectángulo raíz de la aplicación es el Componente delegado. Este es el modelo a partir del cual se construye cada elemento en ListView.
Las variables name, age, type, y size a las que se hace referencia en el delegado proceden de los datos del modelo. Los nombres corresponden a roles definidos en el modelo.
Component { id: dragDelegate Rectangle { id: content required property string name required property string type required property string size required property int age width: view.width height: column.implicitHeight + 4 border.width: 1 border.color: "lightsteelblue" radius: 2 Column { id: column anchors { fill: parent margins: 2 } Text { text: qsTr('Name: ') + content.name } Text { text: qsTr('Type: ') + content.type } Text { text: qsTr('Age: ') + content.age } Text { text: qsTr('Size: ') + content.size } } } }
La segunda parte de la aplicación es el propio ListView al que vinculamos el modelo y el delegado.
ListView { id: view anchors { fill: parent margins: 2 } model: PetsModel {} delegate: dragDelegate spacing: 4 cacheBuffer: 50 }
© 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.