Sophie

Sophie

distrib > Mageia > 6 > x86_64 > by-pkgid > 2e0db81552bc2eca281df795961f7b8f > files > 263

qtdatavis3d5-doc-5.9.4-1.mga6.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" />
<!-- qmllegend.qdoc -->
  <title>Qt Quick 2 Legend Example | Qt Data Visualization 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="qtdatavisualization-index.html">Qt Data Visualization</a></td><td >Qt Quick 2 Legend Example</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">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#legend">Legend</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick 2 Legend Example</h1>
<span class="subtitle"></span>
<!-- $$$qmllegend-description -->
<div class="descr"> <a name="details"></a>
<p>The Qt Quick 2 legend example shows how to make an interactive legend for a graph.</p>
<p class="centerAlign"><img src="images/qmllegend-example.png" alt="" /></p><p>The interesting thing about this example is displaying the legend. We'll concentrate on that and skip explaining the basic functionality - for more detailed QML example documentation, see <a href="qtdatavisualization-qmlscatter-example.html">Qt Quick 2 Scatter Example</a>.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="legend"></a>
<h2 id="legend">Legend</h2>
<p>The legend is simply a column of custom <code>LegendItem</code> items inside a transparent rectangle. Each item is supplied with a series and the graph theme:</p>
<pre class="qml">

  <span class="type">ColumnLayout</span> {
      <span class="name">anchors</span>.fill: <span class="name">parent</span>
      <span class="name">anchors</span>.margins: <span class="name">parent</span>.<span class="name">border</span>.<span class="name">width</span>
      <span class="name">spacing</span>: <span class="number">0</span>
      <span class="name">clip</span>: <span class="number">true</span>
      <span class="type">LegendItem</span> {
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          <span class="name">series</span>: <span class="name">station1</span>
          <span class="name">theme</span>: <span class="name">barGraph</span>.<span class="name">theme</span>
      }
      <span class="type">LegendItem</span> {
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          <span class="name">series</span>: <span class="name">station2</span>
          <span class="name">theme</span>: <span class="name">barGraph</span>.<span class="name">theme</span>
      }
      <span class="type">LegendItem</span> {
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          <span class="name">series</span>: <span class="name">station3</span>
          <span class="name">theme</span>: <span class="name">barGraph</span>.<span class="name">theme</span>
      }
  }

</pre>
<p>The legend items consist of a marker rectangle, which indicates the color of the series, and a text field, which shows the name of the series. The colors we get from the series and the theme supplied at legend item initialization:</p>
<pre class="qml">

  property <span class="type"><a href="qml-qtdatavisualization-theme3d.html">Theme3D</a></span> <span class="name">theme</span>
  property <span class="type"><a href="qml-qtdatavisualization-bar3dseries.html">Bar3DSeries</a></span> <span class="name">series</span>
      ...
  <span class="type">RowLayout</span> {
      <span class="name">anchors</span>.fill: <span class="name">parent</span>
      <span class="name">spacing</span>: <span class="number">0</span>
      <span class="name">clip</span>: <span class="number">true</span>
      <span class="type">Item</span> {
          <span class="name">id</span>: <span class="name">markerSpace</span>
          <span class="name">Layout</span>.minimumWidth: <span class="number">20</span>
          <span class="name">Layout</span>.minimumHeight: <span class="number">20</span>
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          <span class="name">Layout</span>.alignment: <span class="name">Qt</span>.<span class="name">AlignVCenter</span>
          <span class="type">Rectangle</span> {
              <span class="name">x</span>: <span class="name">parent</span>.<span class="name">x</span> <span class="operator">+</span> <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">4</span>
              <span class="name">y</span>: <span class="name">parent</span>.<span class="name">y</span> <span class="operator">+</span> <span class="name">parent</span>.<span class="name">height</span> <span class="operator">/</span> <span class="number">4</span>
              <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
              <span class="name">height</span>: <span class="name">width</span>
              <span class="name">border</span>.color: <span class="string">&quot;black&quot;</span>
              <span class="name">color</span>: <span class="name">series</span>.<span class="name">baseColor</span>
          }
      }
      <span class="type">Item</span> {
          <span class="name">height</span>: <span class="name">markerSpace</span>.<span class="name">height</span>
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          <span class="name">Layout</span>.alignment: <span class="name">Qt</span>.<span class="name">AlignVCenter</span>
          <span class="name">Layout</span>.minimumWidth: <span class="number">100</span>
          <span class="type">Text</span> {
              <span class="name">anchors</span>.fill: <span class="name">parent</span>
              <span class="name">text</span>: <span class="name">series</span>.<span class="name">name</span>
              <span class="name">verticalAlignment</span>: <span class="name">Text</span>.<span class="name">AlignVCenter</span>
              <span class="name">clip</span>: <span class="number">true</span>
              <span class="name">color</span>: <span class="name">theme</span>.<span class="name">labelTextColor</span>
              <span class="name">font</span>: <span class="name">theme</span>.<span class="name">font</span>
          }
      }
  }

</pre>
<p>We want the legend to be interactive, so we add additional logic to enable selection of a series by clicking on a legend item, as well as highlighting the legend item corresponding to the selected series.</p>
<p>The highlight depends on the selection state of the series, so we define two states, which follow the <a href="qml-qtdatavisualization-bar3dseries.html#selectedBar-prop">Bar3DSeries::selectedBar</a> property and adjust the <code>legendItem</code> color appropriately:</p>
<pre class="qml">

  <span class="name">states</span>: [
      <span class="type">State</span>  {
          <span class="name">name</span>: <span class="string">&quot;selected&quot;</span>
          <span class="name">when</span>: <span class="name">series</span>.<span class="name">selectedBar</span> <span class="operator">!=</span> <span class="name">series</span>.<span class="name">invalidSelectionPosition</span>
          <span class="type">PropertyChanges</span> {
              <span class="name">target</span>: <span class="name">legendItem</span>
              <span class="name">color</span>: <span class="name">series</span>.<span class="name">singleHighlightColor</span>
          }
      },
      <span class="type">State</span>  {
          <span class="name">name</span>: <span class="string">&quot;unselected&quot;</span>
          <span class="name">when</span>: <span class="name">series</span>.<span class="name">selectedBar</span> <span class="operator">==</span> <span class="name">series</span>.<span class="name">invalidSelectionPosition</span>
          <span class="type">PropertyChanges</span> {
              <span class="name">target</span>: <span class="name">legendItem</span>
              <span class="name">color</span>: <span class="name">theme</span>.<span class="name">labelBackgroundColor</span>
          }
      }
  ]

</pre>
<p>To make the legend item interactive, we define a MouseArea to detect clicks on it and adjust the series selection accordingly:</p>
<pre class="qml">

  <span class="type">MouseArea</span> {
      <span class="name">id</span>: <span class="name">mouseArea</span>
      <span class="name">anchors</span>.fill: <span class="name">legendItem</span>
      <span class="name">onClicked</span>: {
          <span class="keyword">if</span> (<span class="name">legendItem</span>.<span class="name">state</span> <span class="operator">===</span> <span class="string">&quot;selected&quot;</span>) {
              <span class="name">series</span>.<span class="name">selectedBar</span> <span class="operator">=</span> <span class="name">series</span>.<span class="name">invalidSelectionPosition</span>
          } <span class="keyword">else</span> {
              <span class="name">series</span>.<span class="name">selectedBar</span> <span class="operator">=</span> <span class="name">previousSelection</span>
          }
      }
  }

</pre>
<p>The <code>previousSelection</code> used above is another custom property of <code>LegendItem</code>, which we update whenever selection changes on the series. This way we remember the last selected bar of each series:</p>
<pre class="qml">

  <span class="type">Connections</span> {
      <span class="name">target</span>: <span class="name">series</span>
      <span class="name">onSelectedBarChanged</span>: {
          <span class="keyword">if</span> (<span class="name">position</span> <span class="operator">!=</span> <span class="name">series</span>.<span class="name">invalidSelectionPosition</span>) {
              <span class="name">previousSelection</span> <span class="operator">=</span> <span class="name">position</span>
          }
      }
  }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtdatavisualization-qmllegend-qml-qmllegend-data-qml.html">qmllegend/qml/qmllegend/Data.qml</a></li>
<li><a href="qtdatavisualization-qmllegend-qml-qmllegend-legenditem-qml.html">qmllegend/qml/qmllegend/LegendItem.qml</a></li>
<li><a href="qtdatavisualization-qmllegend-qml-qmllegend-newbutton-qml.html">qmllegend/qml/qmllegend/NewButton.qml</a></li>
<li><a href="qtdatavisualization-qmllegend-qml-qmllegend-main-qml.html">qmllegend/qml/qmllegend/main.qml</a></li>
<li><a href="qtdatavisualization-qmllegend-main-cpp.html">qmllegend/main.cpp</a></li>
<li><a href="qtdatavisualization-qmllegend-qmllegend-pro.html">qmllegend/qmllegend.pro</a></li>
<li><a href="qtdatavisualization-qmllegend-qmllegend-qrc.html">qmllegend/qmllegend.qrc</a></li>
</ul>
</div>
<!-- @@@qmllegend -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>