Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 19952c5e751bf7e3108c3c59625b0f35 > files > 338

qtcharts5-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" />
<!-- examples-legendmarkers.qdoc -->
  <title>LegendMarkers Example | Qt Charts 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="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-examples.html">Qt Charts Examples</a></td><td >LegendMarkers 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="#using-legend-markers">Using Legend Markers</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">LegendMarkers Example</h1>
<span class="subtitle"></span>
<!-- $$$legendmarkers-description -->
<div class="descr"> <a name="details"></a>
<p>In this example we create an application which uses the <a href="qlegendmarker.html">QLegendMarker</a> clicked signal to show/hide the corresponding series in a chart. The connect marker button connects a clicked signal from all markers in a legend to the handleMarkerClicked slot.</p>
<p class="centerAlign"><img src="images/examples_legendmarkers.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="using-legend-markers"></a>
<h2 id="using-legend-markers">Using Legend Markers</h2>
<p>Our application has buttons for adding or removing a series in a chart and a button to connect or disconnect the legend markers clicked signal to our handler. In the image above we have connected the markers and clicked on one of them.</p>
<pre class="cpp">

      <span class="comment">// Connect all markers to handler</span>
      <span class="keyword">const</span> <span class="keyword">auto</span> markers <span class="operator">=</span> m_chart<span class="operator">-</span><span class="operator">&gt;</span>legend()<span class="operator">-</span><span class="operator">&gt;</span>markers();
      <span class="keyword">for</span> (<span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span> <span class="operator">*</span>marker : markers) {
          <span class="comment">// Disconnect possible existing connection to avoid multiple connections</span>
          <span class="type">QObject</span><span class="operator">::</span>disconnect(marker<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span><span class="operator">::</span>clicked<span class="operator">,</span>
                              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWidget<span class="operator">::</span>handleMarkerClicked);
          <span class="type">QObject</span><span class="operator">::</span>connect(marker<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWidget<span class="operator">::</span>handleMarkerClicked);
      }

</pre>
<p>Here we connect the markers in the legend to our handler. To avoid connecting the same marker more than once, we first disconnect it.</p>
<pre class="cpp">

      <span class="keyword">const</span> <span class="keyword">auto</span> markers <span class="operator">=</span> m_chart<span class="operator">-</span><span class="operator">&gt;</span>legend()<span class="operator">-</span><span class="operator">&gt;</span>markers();
      <span class="keyword">for</span> (<span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span> <span class="operator">*</span>marker : markers) {
          <span class="type">QObject</span><span class="operator">::</span>disconnect(marker<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span><span class="operator">::</span>clicked<span class="operator">,</span>
                              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWidget<span class="operator">::</span>handleMarkerClicked);
      }

</pre>
<p>Here we disconnect all markers from our handler.</p>
<pre class="cpp">

      <span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span><span class="operator">*</span> marker <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qlegendmarker.html">QLegendMarker</a></span><span class="operator">*</span><span class="operator">&gt;</span> (sender());
      Q_ASSERT(marker);

</pre>
<p>In our handler we first cast the sender of the event to the <a href="qlegendmarker.html">QLegendMarker</a>.</p>
<pre class="cpp">

      <span class="keyword">switch</span> (marker<span class="operator">-</span><span class="operator">&gt;</span>type())

</pre>
<p>Then we check the type of the marker. This is required if we want to access the detailed methods of the marker and cast it to the correct type. If all we need is the pointer to <a href="qabstractseries.html">QAbstractSeries</a>, the casting isn't necessary. In case of a pie or bar series, we may need the pointer to a related <a href="qpieslice.html">QPieSlice</a> or <a href="qbarset.html">QBarSet</a>.</p>
<pre class="cpp">

          <span class="comment">// Toggle visibility of series</span>
          marker<span class="operator">-</span><span class="operator">&gt;</span>series()<span class="operator">-</span><span class="operator">&gt;</span>setVisible(<span class="operator">!</span>marker<span class="operator">-</span><span class="operator">&gt;</span>series()<span class="operator">-</span><span class="operator">&gt;</span>isVisible());

          <span class="comment">// Turn legend marker back to visible, since hiding series also hides the marker</span>
          <span class="comment">// and we don't want it to happen now.</span>
          marker<span class="operator">-</span><span class="operator">&gt;</span>setVisible(<span class="keyword">true</span>);

</pre>
<p>We want to toggle the visibility of the series, when the marker is clicked. To do so, we get the pointer to a related series from the marker and toggle its visibility. Since the legend marker follows the visibility of the series by default, we also set the marked back to visible. If we don't do so, the marker will be invisible in the legend and we can't click on it anymore.</p>
<pre class="cpp">

          <span class="comment">// Dim the marker, if series is not visible</span>
          <span class="type">qreal</span> alpha <span class="operator">=</span> <span class="number">1.0</span>;

          <span class="keyword">if</span> (<span class="operator">!</span>marker<span class="operator">-</span><span class="operator">&gt;</span>series()<span class="operator">-</span><span class="operator">&gt;</span>isVisible())
              alpha <span class="operator">=</span> <span class="number">0.5</span>;

          <span class="type">QColor</span> color;
          <span class="type">QBrush</span> brush <span class="operator">=</span> marker<span class="operator">-</span><span class="operator">&gt;</span>labelBrush();
          color <span class="operator">=</span> brush<span class="operator">.</span>color();
          color<span class="operator">.</span>setAlphaF(alpha);
          brush<span class="operator">.</span>setColor(color);
          marker<span class="operator">-</span><span class="operator">&gt;</span>setLabelBrush(brush);

          brush <span class="operator">=</span> marker<span class="operator">-</span><span class="operator">&gt;</span>brush();
          color <span class="operator">=</span> brush<span class="operator">.</span>color();
          color<span class="operator">.</span>setAlphaF(alpha);
          brush<span class="operator">.</span>setColor(color);
          marker<span class="operator">-</span><span class="operator">&gt;</span>setBrush(brush);

          <span class="type">QPen</span> pen <span class="operator">=</span> marker<span class="operator">-</span><span class="operator">&gt;</span>pen();
          color <span class="operator">=</span> pen<span class="operator">.</span>color();
          color<span class="operator">.</span>setAlphaF(alpha);
          pen<span class="operator">.</span>setColor(color);
          marker<span class="operator">-</span><span class="operator">&gt;</span>setPen(pen);

</pre>
<p>Instead of making the marker invisible when the series is hidden, we dim the color of the marker. Here we do it by modifying the color of the laberBrush.</p>
<p>Files:</p>
<ul>
<li><a href="qtcharts-legendmarkers-mainwidget-cpp.html">legendmarkers/mainwidget.cpp</a></li>
<li><a href="qtcharts-legendmarkers-mainwidget-h.html">legendmarkers/mainwidget.h</a></li>
<li><a href="qtcharts-legendmarkers-main-cpp.html">legendmarkers/main.cpp</a></li>
<li><a href="qtcharts-legendmarkers-legendmarkers-pro.html">legendmarkers/legendmarkers.pro</a></li>
</ul>
</div>
<!-- @@@legendmarkers -->
        </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>