<?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.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="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"><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="#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-brief --> <p>Showing graph legend in a QML application.</p> <!-- @@@qmllegend --> <!-- $$$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"> ColumnLayout { anchors.fill: parent anchors.margins: parent.border.width spacing: 0 clip: true LegendItem { Layout.fillWidth: true Layout.fillHeight: true series: station1 theme: barGraph.theme } LegendItem { Layout.fillWidth: true Layout.fillHeight: true series: station2 theme: barGraph.theme } LegendItem { Layout.fillWidth: true Layout.fillHeight: true series: station3 theme: barGraph.theme } } </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 Theme3D theme property Bar3DSeries series ... RowLayout { anchors.fill: parent spacing: 0 clip: true Item { id: markerSpace Layout.minimumWidth: 20 Layout.minimumHeight: 20 Layout.fillWidth: true Layout.fillHeight: true Layout.alignment: Qt.AlignVCenter Rectangle { x: parent.x + parent.width / 4 y: parent.y + parent.height / 4 width: parent.width / 2 height: width border.color: "black" color: series.baseColor } } Item { height: markerSpace.height Layout.fillWidth: true Layout.fillHeight: true Layout.alignment: Qt.AlignVCenter Layout.minimumWidth: 100 Text { anchors.fill: parent text: series.name verticalAlignment: Text.AlignVCenter clip: true color: theme.labelTextColor font: theme.font } } } </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"> states: [ State { name: "selected" when: series.selectedBar != series.invalidSelectionPosition PropertyChanges { target: legendItem color: series.singleHighlightColor } }, State { name: "unselected" when: series.selectedBar == series.invalidSelectionPosition PropertyChanges { target: legendItem color: theme.labelBackgroundColor } } ] </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"> MouseArea { id: mouseArea anchors.fill: legendItem onClicked: { if (legendItem.state === "selected") { series.selectedBar = series.invalidSelectionPosition } else { series.selectedBar = previousSelection } } } </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"> Connections { target: series onSelectedBarChanged: { if (position != series.invalidSelectionPosition) { previousSelection = position } } } </pre> <p>Files:</p> <ul> <li><a href="qtdatavisualization-qmllegend-main-cpp.html">qmllegend/main.cpp</a></li> <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-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">©</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>