Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 1500

qtdeclarative5-doc-5.12.6-1.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- modelview.qdoc -->
  <title>Models and Views in Qt Quick | 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 >Models and Views in Qt Quick</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">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#displaying-data-with-views">Displaying Data with Views</a></li>
<li class="level2"><a href="#decorating-views">Decorating Views</a></li>
<li class="level2"><a href="#mouse-and-touch-handling">Mouse and Touch Handling</a></li>
<li class="level2"><a href="#listview-sections">ListView Sections</a></li>
<li class="level1"><a href="#view-delegates">View Delegates</a></li>
<li class="level2"><a href="#accessing-views-and-models-from-delegates">Accessing Views and Models from Delegates</a></li>
<li class="level1"><a href="#models">Models</a></li>
<li class="level2"><a href="#list-model">List Model</a></li>
<li class="level2"><a href="#xml-model">XML Model</a></li>
<li class="level2"><a href="#object-model">Object Model</a></li>
<li class="level2"><a href="#integers-as-models">Integers as Models</a></li>
<li class="level2"><a href="#object-instances-as-models">Object Instances as Models</a></li>
<li class="level2"><a href="#c-data-models">C++ Data Models</a></li>
<li class="level1"><a href="#repeaters">Repeaters</a></li>
<li class="level1"><a href="#changing-model-data">Changing Model Data</a></li>
<li class="level1"><a href="#using-transitions">Using Transitions</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Models and Views in Qt Quick</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-modelviewsdata-modelview.html-description -->
<div class="descr"> <a name="details"></a>
<p>Simply put, applications need to form data and display the data. Qt Quick has the notion of <i>models</i>, <i>views</i>, and <i>delegates</i> to display data. They modularize the visualization of data in order to give the developer or designer control over the different aspects of the data. A developer can swap a list view with a grid view with little changes to the data. Similarly, encapsulating an instance of the data in a delegate allows the developer to dictate how to present or handle the data.</p>
<p class="centerAlign"><img src="images/modelview-overview.png" alt="" /></p><ul>
<li><b>Model</b> - contains the data and its structure. There are several QML types for creating models.</li>
<li><b>View</b> - a container that displays the data. The view might display the data in a list or a grid.</li>
<li><b>Delegate</b> - dictates how the data should appear in the view. The delegate takes each data in the model and encapsulates it. The data is accessible through the delegate. The delegate can also write data back into editable models (e.g&#x2e; in a TextField's onAccepted Handler).</li>
</ul>
<p>To visualize data, bind the view's <code>model</code> property to a model and the <code>delegate</code> property to a component or another compatible type.</p>
<a name="displaying-data-with-views"></a>
<h2 id="displaying-data-with-views">Displaying Data with Views</h2>
<p>Views are containers for collections of items. They are feature-rich and can be customizable to meet style or behavior requirements.</p>
<a name="qtquick-views"></a><p>A set of standard views are provided in the basic set of Qt Quick graphical types:</p>
<ul>
<li><a href="qml-qtquick-listview.html">ListView</a> - arranges items in a horizontal or vertical list</li>
<li><a href="qml-qtquick-gridview.html">GridView</a> - arranges items in a grid within the available space</li>
<li><a href="qml-qtquick-pathview.html">PathView</a> - arranges items on a path</li>
</ul>
<p>These types have properties and behaviors exclusive to each type. Visit their respective documentation for more information.</p>
<a name="decorating-views"></a>
<h3 id="decorating-views">Decorating Views</h3>
<p>Views allow visual customization through <i>decoration</i> properties such as the <code>header</code>, <code>footer</code>, and <code>section</code> properties. By binding an object, usually another visual object, to these properties, the views are decoratable. A footer may include a <a href="qml-qtquick-rectangle.html">Rectangle</a> type showcasing borders or a header that displays a logo on top of the list.</p>
<p>Suppose that a specific club wants to decorate its members list with its brand colors. A member list is in a <code>model</code> and the <code>delegate</code> will display the model's content.</p>
<pre class="qml">

  ListModel {
      id: nameModel
      ListElement { name: "Alice" }
      ListElement { name: "Bob" }
      ListElement { name: "Jane" }
      ListElement { name: "Harry" }
      ListElement { name: "Wendy" }
  }
  Component {
      id: nameDelegate
      Text {
          text: name;
          font.pixelSize: 24
      }
  }

</pre>
<p>The club may decorate the members list by binding visual objects to the <code>header</code> and <code>footer</code> properties. The visual object may be defined inline, in another file, or in a Component type.</p>
<pre class="qml">

  ListView {
      anchors.fill: parent
      clip: true
      model: nameModel
      delegate: nameDelegate
      header: bannercomponent
      footer: Rectangle {
          width: parent.width; height: 30;
          gradient: clubcolors
      }
      highlight: Rectangle {
          width: parent.width
          color: "lightgray"
      }
  }

  Component {     //instantiated when header is processed
      id: bannercomponent
      Rectangle {
          id: banner
          width: parent.width; height: 50
          gradient: clubcolors
          border {color: "#9EDDF2"; width: 2}
          Text {
              anchors.centerIn: parent
              text: "Club Members"
              font.pixelSize: 32
          }
      }
  }
  Gradient {
      id: clubcolors
      GradientStop { position: 0.0; color: "#8EE2FE"}
      GradientStop { position: 0.66; color: "#7ED2EE"}
  }

</pre>
<p class="centerAlign"><img src="images/listview-decorations.png" alt="" /></p><a name="mouse-and-touch-handling"></a>
<h3 id="mouse-and-touch-handling">Mouse and Touch Handling</h3>
<p>The views handle dragging and flicking of their content, however they do not handle touch interaction with the individual delegates. In order for the delegates to react to touch input, e.g&#x2e; to set the <code>currentIndex</code>, a <a href="qml-qtquick-mousearea.html">MouseArea</a> with the appropriate touch handling logic must be provided by the delegate.</p>
<p>Note that if <code>highlightRangeMode</code> is set to <code>StrictlyEnforceRange</code> the currentIndex will be affected by dragging/flicking the view, since the view will always ensure that the <code>currentIndex</code> is within the highlight range specified.</p>
<a name="listview-sections"></a>
<h3 id="listview-sections">ListView Sections</h3>
<p><a href="qml-qtquick-listview.html">ListView</a> contents may be grouped into <i>sections</i>, where related list items are labeled according to their sections. Further, the sections may be decorated with <a href="qtquick-modelviewsdata-modelview.html#qml-view-delegate">delegates</a>.</p>
<p>A list may contain a list indicating people's names and the team on which team the person belongs.</p>
<pre class="qml">

  ListModel {
      id: nameModel
      ListElement { name: "Alice"; team: "Crypto" }
      ListElement { name: "Bob"; team: "Crypto" }
      ListElement { name: "Jane"; team: "QA" }
      ListElement { name: "Victor"; team: "QA" }
      ListElement { name: "Wendy"; team: "Graphics" }
  }
  Component {
      id: nameDelegate
      Text {
          text: name;
          font.pixelSize: 24
          anchors.left: parent.left
          anchors.leftMargin: 2
      }
  }

</pre>
<p>The <a href="qml-qtquick-listview.html">ListView</a> type has the <code>section</code> attached property that can combine adjacent and related types into a section. The <code>section.property</code> determines which list type property to use as sections. The <code>section.criteria</code> can dictate how the section names are displayed and the <code>section.delegate</code> is similar to the views' <a href="qtquick-modelviewsdata-modelview.html#qml-view-delegate">delegate</a> property.</p>
<pre class="qml">

  ListView {
      anchors.fill: parent
      model: nameModel
      delegate: nameDelegate
      focus: true
      highlight: Rectangle {
          color: "lightblue"
          width: parent.width
      }
      section {
          property: "team"
          criteria: ViewSection.FullString
          delegate: Rectangle {
              color: "#b0dfb0"
              width: parent.width
              height: childrenRect.height + 4
              Text { anchors.horizontalCenter: parent.horizontalCenter
                  font.pixelSize: 16
                  font.bold: true
                  text: section
              }
          }
      }
  }

</pre>
<p class="centerAlign"><img src="images/listview-section.png" alt="" /></p><a name="qml-view-delegate"></a><a name="view-delegates"></a>
<h2 id="view-delegates">View Delegates</h2>
<p>Views need a <i>delegate</i> to visually represent an item in a list. A view will visualize each item list according to the template defined by the delegate. Items in a model are accessible through the <code>index</code> property as well as the item's properties.</p>
<pre class="qml">

  Component {
      id: petdelegate
      Text {
          id: label
          font.pixelSize: 24
          text: if (index == 0)
              label.text = type + " (default)"
          else
              text: type
      }
  }

</pre>
<p class="centerAlign"><img src="images/listview-setup.png" alt="" /></p><a name="accessing-views-and-models-from-delegates"></a>
<h3 id="accessing-views-and-models-from-delegates">Accessing Views and Models from Delegates</h3>
<p>The list view to which the delegate is bound is accessible from the delegate through the <code>ListView.view</code> property. Likewise, the <a href="qml-qtquick-gridview.html">GridView</a> <code>GridView.view</code> is available to delegates. The corresponding model and its properties, therefore, are available through <code>ListView.view.model</code>. In addition, any defined signals or methods in the model are also accessible.</p>
<p>This mechanism is useful when you want to use the same delegate for a number of views, for example, but you want decorations or other features to be different for each view, and you would like these different settings to be properties of each of the views. Similarly, it might be of interest to access or show some properties of the model.</p>
<p>In the following example, the delegate shows the property <i>language</i> of the model, and the color of one of the fields depends on the property <i>fruit_color</i> of the view.</p>
<pre class="qml">

  Rectangle {
       width: 200; height: 200

      ListModel {
          id: fruitModel
          property string language: "en"
          ListElement {
              name: "Apple"
              cost: 2.45
          }
          ListElement {
              name: "Orange"
              cost: 3.25
          }
          ListElement {
              name: "Banana"
              cost: 1.95
          }
      }

      Component {
          id: fruitDelegate
          Row {
                  id: fruit
                  Text { text: " Fruit: " + name; color: fruit.ListView.view.fruit_color }
                  Text { text: " Cost: $" + cost }
                  Text { text: " Language: " + fruit.ListView.view.model.language }
          }
      }

      ListView {
          property color fruit_color: "green"
          model: fruitModel
          delegate: fruitDelegate
          anchors.fill: parent
      }
  }

</pre>
<a name="qml-data-models"></a><a name="models"></a>
<h2 id="models">Models</h2>
<p>Data is provided to the delegate via named data roles which the delegate may bind to. Here is a ListModel with two roles, <i>type</i> and <i>age</i>, and a <a href="qml-qtquick-listview.html">ListView</a> with a delegate that binds to these roles to display their values:</p>
<pre class="qml">

  import QtQuick 2.0

  Item {
      width: 200; height: 250

      ListModel {
          id: myModel
          ListElement { type: "Dog"; age: 8 }
          ListElement { type: "Cat"; age: 5 }
      }

      Component {
          id: myDelegate
          Text { text: type + ", " + age }
      }

      ListView {
          anchors.fill: parent
          model: myModel
          delegate: myDelegate
      }
  }

</pre>
<p>If there is a naming clash between the model's properties and the delegate's properties, the roles can be accessed with the qualified <i>model</i> name instead. For example, if a <a href="qml-qtquick-text.html">Text</a> type had <i>type</i> or <i>age</i> properties, the text in the above example would display those property values instead of the <i>type</i> and <i>age</i> values from the model item. In this case, the properties could have been referenced as <code>model.type</code> and <code>model.age</code> instead to ensure the delegate displays the property values from the model item.</p>
<p>A special <i>index</i> role containing the index of the item in the model is also available to the delegate. Note this index is set to -1 if the item is removed from the model. If you bind to the index role, be sure that the logic accounts for the possibility of index being -1, i.e&#x2e; that the item is no longer valid. (Usually the item will shortly be destroyed, but it is possible to delay delegate destruction in some views via a <code>delayRemove</code> attached property.)</p>
<p>Models that do not have named roles (such as the ListModel shown below) will have the data provided via the <i>modelData</i> role. The <i>modelData</i> role is also provided for models that have only one role. In this case the <i>modelData</i> role contains the same data as the named role.</p>
<p>QML provides several types of data models among the built-in set of QML types. In addition, models can be created with Qt C++ and then made available to QQmlEngine for use by QML components. For information about creating these models, visit the <a href="qtquick-modelviewsdata-cppmodels.html">Using C++ Models with Qt Quick Views</a> and creating QML types articles.</p>
<p>Positioning of items from a model can be achieved using a <a href="qml-qtquick-repeater.html">Repeater</a>.</p>
<a name="list-model"></a>
<h3 id="list-model">List Model</h3>
<p>ListModel is a simple hierarchy of types specified in QML. The available roles are specified by the ListElement properties.</p>
<pre class="qml">

  ListModel {
      id: fruitModel

      ListElement {
          name: "Apple"
          cost: 2.45
      }
      ListElement {
          name: "Orange"
          cost: 3.25
      }
      ListElement {
          name: "Banana"
          cost: 1.95
      }
  }

</pre>
<p>The above model has two roles, <i>name</i> and <i>cost</i>. These can be bound to by a <a href="qml-qtquick-listview.html">ListView</a> delegate, for example:</p>
<pre class="qml">

  ListView {
      anchors.fill: parent
      model: fruitModel
      delegate: Row {
          Text { text: "Fruit: " + name }
          Text { text: "Cost: $" + cost }
      }
  }

</pre>
<p>ListModel provides methods to manipulate the ListModel directly via JavaScript. In this case, the first item inserted determines the roles available to any views that are using the model. For example, if an empty ListModel is created and populated via JavaScript, the roles provided by the first insertion are the only roles that will be shown in the view:</p>
<pre class="qml">

  ListModel { id: fruitModel }
      ...
  MouseArea {
      anchors.fill: parent
      onClicked: fruitModel.append({"cost": 5.95, "name":"Pizza"})
  }

</pre>
<p>When the <a href="qml-qtquick-mousearea.html">MouseArea</a> is clicked, <code>fruitModel</code> will have two roles, <i>cost</i> and <i>name</i>. Even if subsequent roles are added, only the first two will be handled by views using the model. To reset the roles available in the model, call ListModel::clear().</p>
<a name="xml-model"></a>
<h3 id="xml-model">XML Model</h3>
<p>XmlListModel allows construction of a model from an XML data source. The roles are specified via the XmlRole type. The type needs to be imported.</p>
<pre class="cpp">

  import <span class="type"><a href="qtquick-module.html">QtQuick</a></span><span class="operator">.</span>XmlListModel <span class="number">2.0</span>

</pre>
<p>The following model has three roles, <i>title</i>, <i>link</i> and <i>description</i>:</p>
<pre class="qml">



</pre>
<p>The <code>query</code> property specifies that the XmlListModel generates a model item for each <code>&lt;item&gt;</code> in the XML document.</p>
<p>The RSS News demo shows how XmlListModel can be used to display an RSS feed.</p>
<a name="object-model"></a>
<h3 id="object-model">Object Model</h3>
<p><a href="qtquick-views-example.html#objectmodel">ObjectModel</a> contains the visual items to be used in a view. When an <a href="qtquick-views-example.html#objectmodel">ObjectModel</a> is used in a view, the view does not require a delegate because the <a href="qtquick-views-example.html#objectmodel">ObjectModel</a> already contains the visual delegate (items).</p>
<p>The example below places three colored rectangles in a <a href="qml-qtquick-listview.html">ListView</a>.</p>
<pre class="cpp">

  import <span class="type"><a href="qtquick-module.html">QtQuick</a></span> <span class="number">2.0</span>
  import <span class="type">QtQml</span><span class="operator">.</span>Models <span class="number">2.1</span>

  Rectangle {
      ObjectModel {
          id: itemModel
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;red&quot;</span> }
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;green&quot;</span> }
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;blue&quot;</span> }
      }

      ListView {
          anchors<span class="operator">.</span>fill: parent
          model: itemModel
      }
  }

</pre>
<a name="integers-as-models"></a>
<h3 id="integers-as-models">Integers as Models</h3>
<p>An integer can be used as a model that contains a certain number of types. In this case, the model does not have any data roles.</p>
<p>The following example creates a <a href="qml-qtquick-listview.html">ListView</a> with five elements:</p>
<pre class="qml">



</pre>
<p><b>Note: </b>The limit on the number of items in an integer model is 100,000,000.</p><a name="object-instances-as-models"></a>
<h3 id="object-instances-as-models">Object Instances as Models</h3>
<p>An object instance can be used to specify a model with a single object type. The properties of the object are provided as roles.</p>
<p>The example below creates a list with one item, showing the color of the <i>myText</i> text. Note the use of the fully qualified <i>model.color</i> property to avoid clashing with <i>color</i> property of the Text type in the delegate.</p>
<pre class="qml">



</pre>
<a name="qml-c-models"></a><a name="c-data-models"></a>
<h3 id="c-data-models">C++ Data Models</h3>
<p>Models can be defined in C++ and then made available to QML. This mechanism is useful for exposing existing C++ data models or otherwise complex datasets to QML.</p>
<p>For information, visit the <a href="qtquick-modelviewsdata-cppmodels.html">Using C++ Models with Qt Quick Views</a> article.</p>
<a name="repeaters"></a>
<h2 id="repeaters">Repeaters</h2>
<div class="float-right"><p><img src="images/repeater-index.png" alt="" /></p>
</div><p>Repeaters create items from a template for use with positioners, using data from a model. Combining repeaters and positioners is an easy way to lay out lots of items. A <a href="qml-qtquick-repeater.html">Repeater</a> item is placed inside a positioner, and generates items that the enclosing positioner arranges.</p>
<p>Each Repeater creates a number of items by combining each element of data from a model, specified using the <a href="qml-qtquick-repeater.html#model-prop">model</a> property, with the template item, defined as a child item within the Repeater. The total number of items is determined by the amount of data in the model.</p>
<p>The following example shows a repeater used with a Grid item to arrange a set of Rectangle items. The Repeater item creates a series of 24 rectangles for the Grid item to position in a 5 by 5 arrangement.</p>
<pre class="qml">

  import QtQuick 2.0

  Rectangle {
      width: 400; height: 400; color: "black"

      Grid {
          x: 5; y: 5
          rows: 5; columns: 5; spacing: 10

          Repeater { model: 24
                     Rectangle { width: 70; height: 70
                                 color: "lightgreen"

                                 Text { text: index
                                        font.pointSize: 30
                                        anchors.centerIn: parent } }
          }
      }
  }

</pre>
<p>The number of items created by a Repeater is held by its <a href="qml-qtquick-repeater.html#count-prop">count</a> property. It is not possible to set this property to determine the number of items to be created. Instead, as in the above example, we use an integer as the model.</p>
<p>For more details, see the <a href="qtquick-modelviewsdata-modelview.html#integers-as-models">QML Data Models</a> document.</p>
<p>If the model is a string list, the delegate is also exposed to a read-only <code>modelData</code> property that holds the string. For example:</p>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td ><pre class="qml">

  Column {
      Repeater {
          model: ["apples", "oranges", "pears"]
          Text { text: "Data: " + modelData }
      }
  }

</pre>
</td><td ><p class="centerAlign"><img src="images/repeater-modeldata.png" alt="" /></p></td></tr>
</table></div>
<p>It is also possible to use a delegate as the template for the items created by a Repeater. This is specified using the <a href="qml-qtquick-repeater.html#delegate-prop">delegate</a> property.</p>
<a name="changing-model-data"></a>
<h2 id="changing-model-data">Changing Model Data</h2>
<p>To change model data, you can assign updated values to the <code>model</code> properties. The QML ListModel is editable by default whereas C++ models must implement setData() to become editable. Integer and JavaScript array models are read-only.</p>
<p>Supposed a QAbstractItemModel based C++ model that implements the setData method is registered as a QML type named <code>EditableModel</code>. Data could then be written to the model like this:</p>
<pre class="qml">



</pre>
<p><b>Note: </b>The <code>edit</code> role is equal to Qt::EditRole. See roleNames() for the built-in role names. However, real life models would usually register custom roles.</p><p>For more information, visit the <a href="qtquick-modelviewsdata-cppmodels.html#changing-model-data">Using C++ Models with Qt Quick Views</a> article.</p>
<a name="using-transitions"></a>
<h2 id="using-transitions">Using Transitions</h2>
<p>Transitions can be used to animate items that are added to, moved within, or removed from a positioner.</p>
<p>Transitions for adding items apply to items that are created as part of a positioner, as well as those that are reparented to become children of a positioner.</p>
<p>Transitions for removing items apply to items within a positioner that are deleted, as well as those that are removed from a positioner and given new parents in a document.</p>
<p><b>Note: </b>Changing the opacity of items to zero will not cause them to disappear from the positioner. They can be removed and re-added by changing the visible property.</p></div>
<!-- @@@qtquick-modelviewsdata-modelview.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>