<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qmlaxisdrag.qdoc --> <title>Qt Quick 2 Axis Dragging 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 Axis Dragging 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="#overriding-default-input-handling">Overriding Default Input Handling</a></li> <li class="level1"><a href="#translating-mouse-movement-to-axis-range-change">Translating Mouse Movement to Axis Range Change</a></li> <li class="level1"><a href="#other-features">Other Features</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 Axis Dragging Example</h1> <span class="subtitle"></span> <!-- $$$qmlaxisdrag-brief --> <p>Implementing axis dragging in QML.</p> <!-- @@@qmlaxisdrag --> <!-- $$$qmlaxisdrag-description --> <div class="descr"> <a name="details"></a> <p>The Qt Quick 2 axis dragging example concentrates on showing how to implement axis range changing by dragging axis labels in QML. It also gives a quick peek to two other new features in Qt Data Visualization 1.1: orthographic projection and dynamic custom item handling.</p> <p class="centerAlign"><img src="images/qmlaxisdrag-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="overriding-default-input-handling"></a> <h2 id="overriding-default-input-handling">Overriding Default Input Handling</h2> <p>First we deactivate the default input handling mechanism by setting the active input handler of <a href="qml-qtdatavisualization-scatter3d.html">Scatter3D</a> graph to <code>null</code>:</p> <pre class="qml"> Scatter3D { id: scatterGraph inputHandler: null ... </pre> <p>Then we add a MouseArea and set it to fill the parent, which is the same <code>Item</code> our <code>scatterGraph</code> is contained in. We also set it to accept only left mouse button presses, as in this example we are not interested in other buttons:</p> <pre class="qml"> MouseArea { anchors.fill: parent hoverEnabled: true acceptedButtons: Qt.LeftButton ... </pre> <p>Then we need to listen to mouse presses, and when caught, send a selection query to the graph:</p> <pre class="qml"> onPressed: { scatterGraph.scene.selectionQueryPosition = Qt.point(mouse.x, mouse.y); } </pre> <p>Current mouse position, that will be needed for move distance calculation, is caught in <code>onPositionChanged</code>:</p> <pre class="qml"> onPositionChanged: { currentMouseX = mouse.x; currentMouseY = mouse.y; ... </pre> <p>At the end of <code>onPositionChanged</code>, we'll save the previous mouse position for move distance calculation that will be introduced later:</p> <pre class="cpp"> ... previousMouseX = currentMouseX; previousMouseY = currentMouseY; } </pre> <a name="translating-mouse-movement-to-axis-range-change"></a> <h2 id="translating-mouse-movement-to-axis-range-change">Translating Mouse Movement to Axis Range Change</h2> <p>in <code>scatterGraph</code> we will need to listen to <code>onSelectedElementChanged</code> signal. The signal is emitted after the selection query has been made in the <code>onPressed</code> of <code>inputArea</code>. We set the element type into a property we defined (<code>property int selectedAxisLabel: -1</code>) in our main component, since it is of a type we are interested in:</p> <pre class="qml"> onSelectedElementChanged: { if (selectedElement >= AbstractGraph3D.ElementAxisXLabel && selectedElement <= AbstractGraph3D.ElementAxisZLabel) selectedAxisLabel = selectedElement else selectedAxisLabel = -1 } </pre> <p>Then, back in the <code>onPositionChanged</code> of <code>inputArea</code>, we check if a mouse button is pressed and if we have a current axis label selection. If the conditions are met, we'll call the function that does the conversion from mouse movement to axis range update:</p> <pre class="cpp"> ... if (pressed && selectedAxisLabel != -1) dragAxis(); ... </pre> <p>The conversion is easy in this case, as we have a fixed camera rotation. We can use some precalculated values, calculate mouse move distance, and apply the values to the selected axis range:</p> <pre class="qml"> function dragAxis() { // Do nothing if previous mouse position is uninitialized if (previousMouseX === -1) return // Directional drag multipliers based on rotation. Camera is locked to 45 degrees, so we // can use one precalculated value instead of calculating xx, xy, zx and zy individually var cameraMultiplier = 0.70710678 // Calculate the mouse move amount var moveX = currentMouseX - previousMouseX var moveY = currentMouseY - previousMouseY // Adjust axes switch (selectedAxisLabel) { case AbstractGraph3D.ElementAxisXLabel: var distance = ((moveX - moveY) * cameraMultiplier) / dragSpeedModifier // Check if we need to change min or max first to avoid invalid ranges if (distance > 0) { scatterGraph.axisX.min -= distance scatterGraph.axisX.max -= distance } else { scatterGraph.axisX.max -= distance scatterGraph.axisX.min -= distance } break case AbstractGraph3D.ElementAxisYLabel: distance = moveY / dragSpeedModifier // Check if we need to change min or max first to avoid invalid ranges if (distance > 0) { scatterGraph.axisY.max += distance scatterGraph.axisY.min += distance } else { scatterGraph.axisY.min += distance scatterGraph.axisY.max += distance } break case AbstractGraph3D.ElementAxisZLabel: distance = ((moveX + moveY) * cameraMultiplier) / dragSpeedModifier // Check if we need to change min or max first to avoid invalid ranges if (distance > 0) { scatterGraph.axisZ.max += distance scatterGraph.axisZ.min += distance } else { scatterGraph.axisZ.min += distance scatterGraph.axisZ.max += distance } break } } </pre> <p>For a more sophisticated conversion from mouse movement to axis range update, see <a href="qtdatavisualization-draggableaxes-example.html">this example</a>.</p> <a name="other-features"></a> <h2 id="other-features">Other Features</h2> <p>The example also demonstrates how to use orthographic projection and how to update properties of a custom item on the fly.</p> <p>Orthographic projection is very simple. You'll just need to change <code>orthoProjection</code> property of <code>scatterGraph</code>. In this example we have a button for toggling it on and off:</p> <pre class="qml"> NewButton { id: orthoToggle width: parent.width / 3 text: "Display Orthographic" anchors.left: rangeToggle.right onClicked: { if (scatterGraph.orthoProjection) { text = "Display Orthographic"; scatterGraph.orthoProjection = false // Orthographic projection disables shadows, so we need to switch them back on scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualityLow } else { text = "Display Perspective"; scatterGraph.orthoProjection = true } } } </pre> <p>For custom items, first we'll add one in the <code>customItemList</code> of <code>scatterGraph</code>:</p> <pre class="qml"> customItemList: [ Custom3DItem { id: qtCube meshFile: ":/mesh/cube" textureFile: ":/texture/texture" position: Qt.vector3d(0.65,0.35,0.65) scaling: Qt.vector3d(0.3,0.3,0.3) } ] </pre> <p>We have implemented a timer to add, remove, and rotate all the items in the graph, and we'll use the same timer for rotating the custom item:</p> <pre class="qml"> onTriggered: { rotationAngle = rotationAngle + 1 qtCube.setRotationAxisAndAngle(Qt.vector3d(1,0,1), rotationAngle) ... </pre> <a name="example-contents"></a> <h2 id="example-contents">Example Contents</h2> <p>Files:</p> <ul> <li><a href="qtdatavisualization-qmlaxisdrag-main-cpp.html">qmlaxisdrag/main.cpp</a></li> <li><a href="qtdatavisualization-qmlaxisdrag-qml-qmlaxisdrag-newbutton-qml.html">qmlaxisdrag/qml/qmlaxisdrag/NewButton.qml</a></li> <li><a href="qtdatavisualization-qmlaxisdrag-qml-qmlaxisdrag-main-qml.html">qmlaxisdrag/qml/qmlaxisdrag/main.qml</a></li> <li><a href="qtdatavisualization-qmlaxisdrag-qmlaxisdrag-pro.html">qmlaxisdrag/qmlaxisdrag.pro</a></li> <li><a href="qtdatavisualization-qmlaxisdrag-qmlaxisdrag-qrc.html">qmlaxisdrag/qmlaxisdrag.qrc</a></li> </ul> <p>Images:</p> <ul> <li><a href="images/used-in-examples/qmlaxisdrag/qml/qmlaxisdrag/cubetexture.png">qmlaxisdrag/qml/qmlaxisdrag/cubetexture.png</a></li> </ul> </div> <!-- @@@qmlaxisdrag --> </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>