<?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.9</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.9</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">Qt 5.9.4 Reference Documentation</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 <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">root</span> <span class="name">width</span>: <span class="number">300</span>; <span class="name">height</span>: <span class="number">400</span> <span class="type">Component</span> { <span class="name">id</span>: <span class="name">dragDelegate</span> <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">content</span> <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> } <span class="name">height</span>: <span class="name">column</span>.<span class="name">implicitHeight</span> <span class="operator">+</span> <span class="number">4</span> <span class="name">border</span>.width: <span class="number">1</span> <span class="name">border</span>.color: <span class="string">"lightsteelblue"</span> <span class="name">radius</span>: <span class="number">2</span> <span class="type"><a href="qml-qtquick-column.html">Column</a></span> { <span class="name">id</span>: <span class="name">column</span> <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Name: '</span> <span class="operator">+</span> <span class="name">name</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Type: '</span> <span class="operator">+</span> <span class="name">type</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Age: '</span> <span class="operator">+</span> <span class="name">age</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Size: '</span> <span class="operator">+</span> <span class="name">size</span> } } } } <span class="type"><a href="qml-qtquick-listview.html">ListView</a></span> { <span class="name">id</span>: <span class="name">view</span> <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> } <span class="name">model</span>: <span class="name">PetsModel</span> {} <span class="name">delegate</span>: <span class="name">dragDelegate</span> <span class="name">spacing</span>: <span class="number">4</span> <span class="name">cacheBuffer</span>: <span class="number">50</span> } } </pre> <p>The model is defined in a separate QML file which looks like this:</p> <pre class="qml"> import QtQuick 2.0 <span class="type">ListModel</span> { <span class="type">ListElement</span> { <span class="name">name</span>: <span class="string">"Polly"</span> <span class="name">type</span>: <span class="string">"Parrot"</span> <span class="name">age</span>: <span class="number">12</span> <span class="name">size</span>: <span class="string">"Small"</span> } <span class="type">ListElement</span> { <span class="name">name</span>: <span class="string">"Penny"</span> <span class="name">type</span>: <span class="string">"Turtle"</span> <span class="name">age</span>: <span class="number">4</span> <span class="name">size</span>: <span class="string">"Small"</span> } } </pre> <a name="walkthrough"></a> <h3 >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"> <span class="type">Component</span> { <span class="name">id</span>: <span class="name">dragDelegate</span> <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">content</span> <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> } <span class="name">height</span>: <span class="name">column</span>.<span class="name">implicitHeight</span> <span class="operator">+</span> <span class="number">4</span> <span class="name">border</span>.width: <span class="number">1</span> <span class="name">border</span>.color: <span class="string">"lightsteelblue"</span> <span class="name">radius</span>: <span class="number">2</span> <span class="type"><a href="qml-qtquick-column.html">Column</a></span> { <span class="name">id</span>: <span class="name">column</span> <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Name: '</span> <span class="operator">+</span> <span class="name">name</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Type: '</span> <span class="operator">+</span> <span class="name">type</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Age: '</span> <span class="operator">+</span> <span class="name">age</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Size: '</span> <span class="operator">+</span> <span class="name">size</span> } } } } </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"> <span class="type"><a href="qml-qtquick-listview.html">ListView</a></span> { <span class="name">id</span>: <span class="name">view</span> <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> } <span class="name">model</span>: <span class="name">PetsModel</span> {} <span class="name">delegate</span>: <span class="name">dragDelegate</span> <span class="name">spacing</span>: <span class="number">4</span> <span class="name">cacheBuffer</span>: <span class="number">50</span> } </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> 2017 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>