<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- dynamicview-tutorial.qdoc --> <title>QML Dynamic View Ordering Tutorial 1 - A Simple ListView and Delegate | Qt Quick 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td >QML Dynamic View Ordering Tutorial 1 - A Simple ListView and Delegate</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <link rel="prev" href="qml-dynamicview-tutorial.html" /> <link rel="next" href="qtquick-tutorials-dynamicview-dynamicview2-example.html" /> <p class="naviNextPrevious headerNavi"> <a class="prevPage" href="qml-dynamicview-tutorial.html">QML Dynamic View Ordering Tutorial</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qtquick-tutorials-dynamicview-dynamicview2-example.html">QML Dynamic View Ordering Tutorial 2 - Dragging View Items</a> </p><p/> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level2"><a href="#walkthrough">Walkthrough</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QML Dynamic View Ordering Tutorial 1 - A Simple ListView and Delegate</h1> <span class="subtitle"></span> <!-- $$$tutorials/dynamicview/dynamicview1-description --> <div class="descr"> <a name="details"></a> <p>We begin our application by defining a <a href="qml-qtquick-listview.html">ListView</a>, a model which will provide data to the view, and a delegate which provides a template for constructing items in the view.</p> <p>The code for the <a href="qml-qtquick-listview.html">ListView</a> and delegate looks like this:</p> <pre class="qml"> import QtQuick 2.0 Rectangle { id: root width: 300; height: 400 Component { id: dragDelegate Rectangle { id: content anchors { left: parent.left; right: parent.right } height: column.implicitHeight + 4 border.width: 1 border.color: "lightsteelblue" radius: 2 Column { id: column anchors { fill: parent; margins: 2 } Text { text: 'Name: ' + name } Text { text: 'Type: ' + type } Text { text: 'Age: ' + age } Text { text: 'Size: ' + size } } } } ListView { id: view anchors { fill: parent; margins: 2 } model: PetsModel {} delegate: dragDelegate spacing: 4 cacheBuffer: 50 } } </pre> <p>The model is defined in a separate QML file which looks like this:</p> <pre class="qml"> import QtQuick 2.0 ListModel { ListElement { name: "Polly" type: "Parrot" age: 12 size: "Small" } ListElement { name: "Penny" type: "Turtle" age: 4 size: "Small" } } </pre> <a name="walkthrough"></a> <h3 id="walkthrough">Walkthrough</h3> <p>The first item defined within the application's root Rectangle is the delegate Component. This is the template from which each item in the <a href="qml-qtquick-listview.html">ListView</a> is constructed.</p> <p>The <code>name</code>, <code>age</code>, <code>type</code>, and <code>size</code> variables referenced in the delegate are sourced from the model data. The names correspond to roles defined in the model.</p> <pre class="qml"> Component { id: dragDelegate Rectangle { id: content anchors { left: parent.left; right: parent.right } height: column.implicitHeight + 4 border.width: 1 border.color: "lightsteelblue" radius: 2 Column { id: column anchors { fill: parent; margins: 2 } Text { text: 'Name: ' + name } Text { text: 'Type: ' + type } Text { text: 'Age: ' + age } Text { text: 'Size: ' + size } } } } </pre> <p>The second part of the application is the <a href="qml-qtquick-listview.html">ListView</a> itself to which we bind the model and delegate.</p> <pre class="qml"> ListView { id: view anchors { fill: parent; margins: 2 } model: PetsModel {} delegate: dragDelegate spacing: 4 cacheBuffer: 50 } </pre> <p>Files:</p> <ul> <li><a href="qtquick-tutorials-dynamicview-dynamicview1-petsmodel-qml.html">tutorials/dynamicview/dynamicview1/PetsModel.qml</a></li> <li><a href="qtquick-tutorials-dynamicview-dynamicview1-dynamicview-qml.html">tutorials/dynamicview/dynamicview1/dynamicview.qml</a></li> <li><a href="qtquick-tutorials-dynamicview-dynamicview1-dynamicview1-qmlproject.html">tutorials/dynamicview/dynamicview1/dynamicview1.qmlproject</a></li> </ul> </div> <!-- @@@tutorials/dynamicview/dynamicview1 --> <p class="naviNextPrevious footerNavi"> <a class="prevPage" href="qml-dynamicview-tutorial.html">QML Dynamic View Ordering Tutorial</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qtquick-tutorials-dynamicview-dynamicview2-example.html">QML Dynamic View Ordering Tutorial 2 - Dragging View Items</a> </p> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> 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. </p> </div> </body> </html>