<?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.12.2</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="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"><a href="qtdatavisualization-index.html">Qt Data Visualization | Commercial or GPLv3</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="#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-brief --> <p>Using <a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a> in a QML application.</p> <!-- @@@qmlscatter --> <!-- $$$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">&</span><span class="type">QQmlEngine</span><span class="operator">::</span>quit<span class="operator">,</span> <span class="operator">&</span>viewer<span class="operator">,</span> <span class="operator">&</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"><</span>RCC<span class="operator">></span> <span class="operator"><</span>qresource prefix<span class="operator">=</span><span class="string">"/"</span><span class="operator">></span> <span class="operator"><</span>file<span class="operator">></span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>Data<span class="operator">.</span>qml<span class="operator"><</span><span class="operator">/</span>file<span class="operator">></span> <span class="operator"><</span>file<span class="operator">></span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>main<span class="operator">.</span>qml<span class="operator"><</span><span class="operator">/</span>file<span class="operator">></span> <span class="operator"><</span>file<span class="operator">></span>qml<span class="operator">/</span>qmlscatter<span class="operator">/</span>NewButton<span class="operator">.</span>qml<span class="operator"><</span><span class="operator">/</span>file<span class="operator">></span> <span class="operator"><</span><span class="operator">/</span>qresource<span class="operator">></span> <span class="operator"><</span><span class="operator">/</span>RCC<span class="operator">></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">"qrc:/qml/qmlscatter/main.qml"</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 "." </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"> Rectangle { id: mainView </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"> Item { id: dataView anchors.bottom: parent.bottom </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"> Scatter3D { id: scatterGraph width: dataView.width height: dataView.height </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"> theme: themeIsabelle shadowQuality: AbstractGraph3D.ShadowQualitySoftLow </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"> Theme3D { id: themeIsabelle type: Theme3D.ThemeIsabelle font.family: "Lucida Handwriting" font.pointSize: 40 } </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"> Data { id: seriesData } </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"> ListModel { id: dataModel ListElement{ xPos: -10.0; yPos: 5.0; zPos: -5.0 } ... </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 alias model: dataModel property alias modelTwo: dataModelTwo property alias modelThree: dataModelThree </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"> Scatter3DSeries { id: scatterSeries </pre> <p>Then we'll set up selection label format for the series:</p> <pre class="qml"> itemLabelFormat: "Series 1: X:@xLabel Y:@yLabel Z:@zLabel" </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"> ItemModelScatterDataProxy { itemModel: seriesData.model xPosRole: "xPos" yPosRole: "yPos" zPosRole: "zPos" } </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"> Scatter3DSeries { id: scatterSeriesTwo itemLabelFormat: "Series 2: X:@xLabel Y:@yLabel Z:@zLabel" itemSize: 0.1 mesh: Abstract3DSeries.MeshCube ... </pre> <p>Then we'll modify the properties of the default axes in <code>scatterGraph</code> a bit:</p> <pre class="qml"> axisX.segmentCount: 3 axisX.subSegmentCount: 2 axisX.labelFormat: "%.2f" axisZ.segmentCount: 2 axisZ.subSegmentCount: 2 axisZ.labelFormat: "%.2f" axisY.segmentCount: 2 axisY.subSegmentCount: 2 axisY.labelFormat: "%.2f" </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"> NewButton { id: shadowToggle Layout.fillHeight: true Layout.fillWidth: true text: scatterGraph.shadowsSupported ? "Hide Shadows" : "Shadows not supported" enabled: scatterGraph.shadowsSupported onClicked: { if (scatterGraph.shadowQuality === AbstractGraph3D.ShadowQualityNone) { scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualitySoftLow; text = "Hide Shadows"; } else { scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualityNone; text = "Show Shadows"; } } } </pre> <p>Then we'll modify <code>dataView</code> to make room for the buttons at the top:</p> <pre class="qml"> Item { id: dataView anchors.bottom: parent.bottom width: parent.width height: parent.height - buttonLayout.height ... </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-main-cpp.html">qmlscatter/main.cpp</a></li> <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-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">©</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>