Sophie

Sophie

distrib > Mageia > 6 > armv7hl > by-pkgid > 2e0db81552bc2eca281df795961f7b8f > files > 287

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" />
<!-- qmlscatter.qdoc -->
  <title>Qt Quick 2 Scatter 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 Scatter 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="#application-basics">Application Basics</a></li>
<li class="level1"><a href="#setting-up-the-graph">Setting up the Graph</a></li>
<li class="level1"><a href="#adding-data-to-the-graph">Adding Data to the Graph</a></li>
<li class="level1"><a href="#example-contents">Example Contents</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick 2 Scatter Example</h1>
<span class="subtitle"></span>
<!-- $$$qmlscatter-description -->
<div class="descr"> <a name="details"></a>
<p>The Qt Quick 2 scatter example shows how to make a simple scatter graph visualization using <a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a> and Qt Quick 2.</p>
<p>For instructions about how to interact with the graph, see <a href="qtdatavisualization-interacting-with-data.html">this page</a>.</p>
<p>For instructions how to create a new Qt Quick 2 application of your own, see Qt Creator help.</p>
<p class="centerAlign"><img src="images/qmlscatter-example.png" alt="" /></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="application-basics"></a>
<h2 id="application-basics">Application Basics</h2>
<p>Before diving into the QML code, let's take a look at the application <code>main.cpp</code>.</p>
<p>This application implements a 'Quit' button in the UI, so we want to connect the QQmlEngine::quit() signal to our application's QWindow::close() slot:</p>
<pre class="cpp">

  <span class="type">QObject</span><span class="operator">::</span>connect(viewer<span class="operator">.</span>engine()<span class="operator">,</span> <span class="operator">&amp;</span><span class="type">QQmlEngine</span><span class="operator">::</span>quit<span class="operator">,</span> <span class="operator">&amp;</span>viewer<span class="operator">,</span> <span class="operator">&amp;</span><span class="type">QWindow</span><span class="operator">::</span>close);

</pre>
<p>To make deployment little simpler, we gather all of the application's <code>.qml</code> files to a resource file (<code>qmlscatter.qrc</code>):</p>
<pre class="cpp">

  <span class="operator">&lt;</span>RCC<span class="operator">&gt;</span>
      <span class="operator">&lt;</span>qresource prefix<span class="operator">=</span><span class="string">&quot;/&quot;</span><span class="operator">&gt;</span>
          <span class="operator">&lt;</span>file<span class="operator">&gt;</span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>Data<span class="operator">.</span>qml<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
          <span class="operator">&lt;</span>file<span class="operator">&gt;</span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>main<span class="operator">.</span>qml<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
          <span class="operator">&lt;</span>file<span class="operator">&gt;</span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>NewButton<span class="operator">.</span>qml<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
      <span class="operator">&lt;</span><span class="operator">/</span>qresource<span class="operator">&gt;</span>
  <span class="operator">&lt;</span><span class="operator">/</span>RCC<span class="operator">&gt;</span>

</pre>
<p>This also requires us to set the <code>main.qml</code> to be read from the resource (<code>qrc:</code>):</p>
<pre class="cpp">

  viewer<span class="operator">.</span>setSource(<span class="type">QUrl</span>(<span class="string">&quot;qrc:/qml/qmlscatter/main.qml&quot;</span>));

</pre>
<p>Lastly, we want the application to run in a maximized window:</p>
<pre class="cpp">

  viewer<span class="operator">.</span>showMaximized();

</pre>
<a name="setting-up-the-graph"></a>
<h2 id="setting-up-the-graph">Setting up the Graph</h2>
<p>First we'll import all the QML modules we need:</p>
<pre class="qml">

  import QtQuick 2.1
  import QtQuick.Layouts 1.0
  import QtDataVisualization 1.0
  import &quot;.&quot;

</pre>
<p>The last <code>import</code> just imports all the qml files in the same directory as our <code>main.qml</code>, because that's where <code>NewButton.qml</code> and <code>Data.qml</code> are.</p>
<p>Then we create our main <code>Rectangle</code> and call it <code>mainView</code>:</p>
<pre class="qml">

  <span class="type">Rectangle</span> {
      <span class="name">id</span>: <span class="name">mainView</span>

</pre>
<p>Then we'll add another <code>Item</code> inside the main <code>Rectangle</code>, and call it <code>dataView</code>. This will be the item to hold the <a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a> graph. We'll anchor it to the parent bottom:</p>
<pre class="qml">

  <span class="type">Item</span> {
      <span class="name">id</span>: <span class="name">dataView</span>
      <span class="name">anchors</span>.bottom: <span class="name">parent</span>.<span class="name">bottom</span>

</pre>
<p>Next we're ready to add the <a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a> graph itself. We'll add it inside the <code>dataView</code> and name it <code>scatterGraph</code>. Let's make it fill the <code>dataView</code>:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a></span> {
      <span class="name">id</span>: <span class="name">scatterGraph</span>
      <span class="name">width</span>: <span class="name">dataView</span>.<span class="name">width</span>
      <span class="name">height</span>: <span class="name">dataView</span>.<span class="name">height</span>

</pre>
<p>Now the graph is ready for use, but has no data. It also has the default axes and visual properties.</p>
<p>Let's modify some visual properties first by adding the following inside <code>scatterGraph</code>:</p>
<pre class="qml">

  <span class="name">theme</span>: <span class="name">themeIsabelle</span>
  <span class="name">shadowQuality</span>: <span class="name">AbstractGraph3D</span>.<span class="name">ShadowQualitySoftLow</span>

</pre>
<p>We added a customized theme and changed the shadow quality. We're happy with the other visual properties, so we won't change them.</p>
<p>The custom theme is based on a predefined theme, but we change the font in it:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtdatavisualization-theme3d.html">Theme3D</a></span> {
      <span class="name">id</span>: <span class="name">themeIsabelle</span>
      <span class="name">type</span>: <span class="name">Theme3D</span>.<span class="name">ThemeIsabelle</span>
      <span class="name">font</span>.family: <span class="string">&quot;Lucida Handwriting&quot;</span>
      <span class="name">font</span>.pointSize: <span class="number">40</span>
  }

</pre>
<p>Then it's time to start feeding the graph some data.</p>
<a name="adding-data-to-the-graph"></a>
<h2 id="adding-data-to-the-graph">Adding Data to the Graph</h2>
<p>Let's create a <code>Data</code> item inside the <code>mainView</code> and name it <code>seriesData</code>:</p>
<pre class="qml">

  <span class="type">Data</span> {
      <span class="name">id</span>: <span class="name">seriesData</span>
  }

</pre>
<p>The <code>seriesData</code> item contains the data models for all three series we use in this example.</p>
<p>This is the component that holds our data in <code>Data.qml</code>. It has an <code>Item</code> as the main component.</p>
<p>In the main component we'll add the data itself in a <code>ListModel</code> and name it <code>dataModel</code>:</p>
<pre class="qml">

  <span class="type">ListModel</span> {
      <span class="name">id</span>: <span class="name">dataModel</span>
      <span class="type">ListElement</span>{ <span class="name">xPos</span>: -<span class="number">10.0</span>; <span class="name">yPos</span>: <span class="number">5.0</span>; <span class="name">zPos</span>: -<span class="number">5.0</span> }
      ...

</pre>
<p>We'll add two more of these for the other two series, and name them <code>dataModelTwo</code> and <code>dataModelThree</code>.</p>
<p>Then we need to expose the data models to be usable from <code>main.qml</code>. We do this by defining them as aliases in the main data component:</p>
<pre class="qml">

  property <span class="type">alias</span> <span class="name">model</span>: <span class="name">dataModel</span>
  property <span class="type">alias</span> <span class="name">modelTwo</span>: <span class="name">dataModelTwo</span>
  property <span class="type">alias</span> <span class="name">modelThree</span>: <span class="name">dataModelThree</span>

</pre>
<p>Now we can use the data from <code>Data.qml</code> with <code>scatterGraph</code> in <code>main.qml</code>. First we'll add a <a href="qml-qtdatavisualization-scatter3dseries.html">Scatter3DSeries</a> and call it <code>scatterSeries</code>:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtdatavisualization-scatter3dseries.html">Scatter3DSeries</a></span> {
      <span class="name">id</span>: <span class="name">scatterSeries</span>

</pre>
<p>Then we'll set up selection label format for the series:</p>
<pre class="qml">

  <span class="name">itemLabelFormat</span>: <span class="string">&quot;Series 1: X:@xLabel Y:@yLabel Z:@zLabel&quot;</span>

</pre>
<p>And finally the data for series one in a <a href="qml-qtdatavisualization-itemmodelscatterdataproxy.html">ItemModelScatterDataProxy</a>. We set the data itself as <code>itemModel</code> for the proxy:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtdatavisualization-itemmodelscatterdataproxy.html">ItemModelScatterDataProxy</a></span> {
      <span class="name">itemModel</span>: <span class="name">seriesData</span>.<span class="name">model</span>
      <span class="name">xPosRole</span>: <span class="string">&quot;xPos&quot;</span>
      <span class="name">yPosRole</span>: <span class="string">&quot;yPos&quot;</span>
      <span class="name">zPosRole</span>: <span class="string">&quot;zPos&quot;</span>
  }

</pre>
<p>We'll add the other two series in the same way, but modify some series-specific details a bit:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtdatavisualization-scatter3dseries.html">Scatter3DSeries</a></span> {
      <span class="name">id</span>: <span class="name">scatterSeriesTwo</span>
      <span class="name">itemLabelFormat</span>: <span class="string">&quot;Series 2: X:@xLabel Y:@yLabel Z:@zLabel&quot;</span>
      <span class="name">itemSize</span>: <span class="number">0.1</span>
      <span class="name">mesh</span>: <span class="name">Abstract3DSeries</span>.<span class="name">MeshCube</span>
      ...

</pre>
<p>Then we'll modify the properties of the default axes in <code>scatterGraph</code> a bit:</p>
<pre class="qml">

  <span class="name">axisX</span>.segmentCount: <span class="number">3</span>
  <span class="name">axisX</span>.subSegmentCount: <span class="number">2</span>
  <span class="name">axisX</span>.labelFormat: <span class="string">&quot;%.2f&quot;</span>
  <span class="name">axisZ</span>.segmentCount: <span class="number">2</span>
  <span class="name">axisZ</span>.subSegmentCount: <span class="number">2</span>
  <span class="name">axisZ</span>.labelFormat: <span class="string">&quot;%.2f&quot;</span>
  <span class="name">axisY</span>.segmentCount: <span class="number">2</span>
  <span class="name">axisY</span>.subSegmentCount: <span class="number">2</span>
  <span class="name">axisY</span>.labelFormat: <span class="string">&quot;%.2f&quot;</span>

</pre>
<p>After that we'll just add a few buttons to the <code>mainView</code> to control the graph. We'll only show one as an example:</p>
<pre class="qml">

  <span class="type">NewButton</span> {
      <span class="name">id</span>: <span class="name">shadowToggle</span>
      <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
      <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
      <span class="name">text</span>: <span class="name">scatterGraph</span>.<span class="name">shadowsSupported</span> ? <span class="string">&quot;Hide Shadows&quot;</span> : <span class="string">&quot;Shadows not supported&quot;</span>
      <span class="name">enabled</span>: <span class="name">scatterGraph</span>.<span class="name">shadowsSupported</span>
      <span class="name">onClicked</span>: {
          <span class="keyword">if</span> (<span class="name">scatterGraph</span>.<span class="name">shadowQuality</span> <span class="operator">===</span> <span class="name">AbstractGraph3D</span>.<span class="name">ShadowQualityNone</span>) {
              <span class="name">scatterGraph</span>.<span class="name">shadowQuality</span> <span class="operator">=</span> <span class="name">AbstractGraph3D</span>.<span class="name">ShadowQualitySoftLow</span>;
              <span class="name">text</span> <span class="operator">=</span> <span class="string">&quot;Hide Shadows&quot;</span>;
          } <span class="keyword">else</span> {
              <span class="name">scatterGraph</span>.<span class="name">shadowQuality</span> <span class="operator">=</span> <span class="name">AbstractGraph3D</span>.<span class="name">ShadowQualityNone</span>;
              <span class="name">text</span> <span class="operator">=</span> <span class="string">&quot;Show Shadows&quot;</span>;
          }
      }
  }

</pre>
<p>Then we'll modify <code>dataView</code> to make room for the buttons at the top:</p>
<pre class="qml">

  <span class="type">Item</span> {
      <span class="name">id</span>: <span class="name">dataView</span>
      <span class="name">anchors</span>.bottom: <span class="name">parent</span>.<span class="name">bottom</span>
      <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
      <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">-</span> <span class="name">buttonLayout</span>.<span class="name">height</span>
      ...

</pre>
<p>And we're done!</p>
<a name="example-contents"></a>
<h2 id="example-contents">Example Contents</h2>
<p>Files:</p>
<ul>
<li><a href="qtdatavisualization-qmlscatter-qml-qmlscatter-data-qml.html">qmlscatter/qml/qmlscatter/Data.qml</a></li>
<li><a href="qtdatavisualization-qmlscatter-qml-qmlscatter-newbutton-qml.html">qmlscatter/qml/qmlscatter/NewButton.qml</a></li>
<li><a href="qtdatavisualization-qmlscatter-qml-qmlscatter-main-qml.html">qmlscatter/qml/qmlscatter/main.qml</a></li>
<li><a href="qtdatavisualization-qmlscatter-main-cpp.html">qmlscatter/main.cpp</a></li>
<li><a href="qtdatavisualization-qmlscatter-qmlscatter-pro.html">qmlscatter/qmlscatter.pro</a></li>
<li><a href="qtdatavisualization-qmlscatter-qmlscatter-qrc.html">qmlscatter/qmlscatter.qrc</a></li>
</ul>
</div>
<!-- @@@qmlscatter -->
        </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>