Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > dbf69eaa13c86c67c80d31d9cbb7847c > files > 4610

qtbase5-doc-5.9.4-1.2.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" />
<!-- wiggly.qdoc -->
  <title>Wiggly Example | Qt Widgets 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="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-widgets.html">Qt Widgets Examples</a></td><td >Wiggly 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="#dialog-class-definition">Dialog Class Definition</a></li>
<li class="level1"><a href="#dialog-class-implementation">Dialog Class Implementation</a></li>
<li class="level1"><a href="#wigglywidget-class-definition">WigglyWidget Class Definition</a></li>
<li class="level1"><a href="#wigglywidget-class-implementation">WigglyWidget Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Wiggly Example</h1>
<span class="subtitle"></span>
<!-- $$$widgets/wiggly-description -->
<div class="descr"> <a name="details"></a>
<div class="border"><p class="centerAlign"><img src="images/wiggly-example.png" alt="" /></p></div><p class="figCaption">Screenshot of the Wiggly example</p>
<p><a href="../qtcore/qbasictimer.html">QBasicTimer</a> is a low-level class for timers. Unlike <a href="../qtcore/qtimer.html">QTimer</a>, <a href="../qtcore/qbasictimer.html">QBasicTimer</a> doesn't inherit from <a href="../qtcore/qobject.html">QObject</a>; instead of emitting a <a href="../qtcore/qtimer.html#timeout">timeout()</a> signal when a certain amount of time has passed, it sends a <a href="../qtcore/qtimerevent.html">QTimerEvent</a> to a <a href="../qtcore/qobject.html">QObject</a> of our choice. This makes <a href="../qtcore/qbasictimer.html">QBasicTimer</a> a more lightweight alternative to <a href="../qtcore/qtimer.html">QTimer</a>. Qt's built-in widgets use it internally, and it is provided in Qt's API for highly-optimized applications (such as embedded applications).</p>
<p>The example consists of two classes:</p>
<ul>
<li><code>WigglyWidget</code> is the custom widget displaying the text in a wiggly line.</li>
<li><code>Dialog</code> is the dialog widget allowing the user to enter a text. It combines a <code>WigglyWidget</code> and a <code>QLineEdit</code>.</li>
</ul>
<p>We will first take a quick look at the <code>Dialog</code> class, then we will review the <code>WigglyWidget</code> class.</p>
<a name="dialog-class-definition"></a>
<h2 id="dialog-class-definition">Dialog Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> Dialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> Dialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
  };

</pre>
<p>The <code>Dialog</code> class provides a dialog widget that allows the user to enter a text. The text is then rendered by <code>WigglyWidget</code>.</p>
<a name="dialog-class-implementation"></a>
<h2 id="dialog-class-implementation">Dialog Class Implementation</h2>
<pre class="cpp">

  Dialog<span class="operator">::</span>Dialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qdialog.html">QDialog</a></span>(parent)
  {
      WigglyWidget <span class="operator">*</span>wigglyWidget <span class="operator">=</span> <span class="keyword">new</span> WigglyWidget;
      <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>;

      <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(wigglyWidget);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(lineEdit);

      connect(lineEdit<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">::</span>textChanged<span class="operator">,</span> wigglyWidget<span class="operator">,</span> <span class="operator">&amp;</span>WigglyWidget<span class="operator">::</span>setText);
      lineEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Hello world!&quot;</span>));

      setWindowTitle(tr(<span class="string">&quot;Wiggly&quot;</span>));
      resize(<span class="number">360</span><span class="operator">,</span> <span class="number">145</span>);
  }

</pre>
<p>In the constructor we create a wiggly widget along with a <a href="qlineedit.html">line edit</a>, and we put the two widgets in a vertical layout. We connect the line edit's <a href="qlineedit.html#textChanged">textChanged()</a> signal to the wiggly widget's <code>setText()</code> slot to obtain the real time interaction with the wiggly widget. The widget's default text is &quot;Hello world!&quot;.</p>
<a name="wigglywidget-class-definition"></a>
<h2 id="wigglywidget-class-definition">WigglyWidget Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> WigglyWidget : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      WigglyWidget(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> setText(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>newText) { text <span class="operator">=</span> newText; }

  <span class="keyword">protected</span>:
      <span class="type">void</span> paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event) override;
      <span class="type">void</span> timerEvent(<span class="type"><a href="../qtcore/qtimerevent.html">QTimerEvent</a></span> <span class="operator">*</span>event) override;

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtcore/qbasictimer.html">QBasicTimer</a></span> timer;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text;
      <span class="type">int</span> step;
  };

</pre>
<p>The <code>WigglyWidget</code> class provides the wiggly line displaying the text. We subclass <a href="qwidget.html">QWidget</a> and reimplement the standard <a href="qwidget.html#paintEvent">paintEvent()</a> and <a href="../qtcore/qobject.html#timerEvent">timerEvent()</a> functions to draw and update the widget. In addition we implement a public <code>setText()</code> slot that sets the widget's text.</p>
<p>The <code>timer</code> variable, of type <a href="../qtcore/qbasictimer.html">QBasicTimer</a>, is used to update the widget at regular intervals, making the widget move. The <code>text</code> variable is used to store the currently displayed text, and <code>step</code> to calculate position and color for each character on the wiggly line.</p>
<a name="wigglywidget-class-implementation"></a>
<h2 id="wigglywidget-class-implementation">WigglyWidget Class Implementation</h2>
<pre class="cpp">

  WigglyWidget<span class="operator">::</span>WigglyWidget(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
  {
      setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Midlight);
      setAutoFillBackground(<span class="keyword">true</span>);

      <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> newFont <span class="operator">=</span> font();
      newFont<span class="operator">.</span>setPointSize(newFont<span class="operator">.</span>pointSize() <span class="operator">+</span> <span class="number">20</span>);
      setFont(newFont);

      step <span class="operator">=</span> <span class="number">0</span>;
      timer<span class="operator">.</span>start(<span class="number">60</span><span class="operator">,</span> <span class="keyword">this</span>);
  }

</pre>
<p>In the constructor, we make the widget's background slightly lighter than the usual background using the <a href="../qtgui/qpalette.html#ColorRole-enum">QPalette::Midlight</a> color role. The background role defines the brush from the widget's palette that Qt uses to paint the background. Then we enlarge the widget's font with 20 points.</p>
<p>Finally we start the timer; the call to <a href="../qtcore/qbasictimer.html#start">QBasicTimer::start</a>() makes sure that <i>this</i> particular wiggly widget will receive the timer events generated when the timer times out (every 60 milliseconds).</p>
<pre class="cpp">

  <span class="type">void</span> WigglyWidget<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span> <span class="comment">/* event */</span>)
  {
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">int</span> sineTable<span class="operator">[</span><span class="number">16</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="number">0</span><span class="operator">,</span> <span class="number">38</span><span class="operator">,</span> <span class="number">71</span><span class="operator">,</span> <span class="number">92</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">92</span><span class="operator">,</span> <span class="number">71</span><span class="operator">,</span> <span class="number">38</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">38</span><span class="operator">,</span> <span class="operator">-</span><span class="number">71</span><span class="operator">,</span> <span class="operator">-</span><span class="number">92</span><span class="operator">,</span> <span class="operator">-</span><span class="number">100</span><span class="operator">,</span> <span class="operator">-</span><span class="number">92</span><span class="operator">,</span> <span class="operator">-</span><span class="number">71</span><span class="operator">,</span> <span class="operator">-</span><span class="number">38</span>
      };

      <span class="type"><a href="../qtgui/qfontmetrics.html">QFontMetrics</a></span> metrics(font());
      <span class="type">int</span> x <span class="operator">=</span> (width() <span class="operator">-</span> metrics<span class="operator">.</span>width(text)) <span class="operator">/</span> <span class="number">2</span>;
      <span class="type">int</span> y <span class="operator">=</span> (height() <span class="operator">+</span> metrics<span class="operator">.</span>ascent() <span class="operator">-</span> metrics<span class="operator">.</span>descent()) <span class="operator">/</span> <span class="number">2</span>;
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color;

</pre>
<p>The <code>paintEvent()</code> function is called whenever a <a href="../qtgui/qpaintevent.html">QPaintEvent</a> is sent to the widget. Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved. For the wiggly widget, a paint event will also be generated every 60 milliseconds from the <code>timerEvent()</code> slot.</p>
<p>The <code>sineTable</code> represents y-values of the sine curve, multiplied by 100. It is used to make the wiggly widget move along the sine curve.</p>
<p>The <a href="../qtgui/qfontmetrics.html">QFontMetrics</a> object provides information about the widget's font. The <code>x</code> variable is the horizontal position where we start drawing the text. The <code>y</code> variable is the vertical position of the text's base line. Both variables are computed so that the text is horizontally and vertically centered. To compute the base line, we take into account the font's ascent (the height of the font above the base line) and font's descent (the height of the font below the base line). If the descent equals the ascent, they cancel out each other and the base line is at <code>height()</code> / 2.</p>
<pre class="cpp">

      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> text<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type">int</span> index <span class="operator">=</span> (step <span class="operator">+</span> i) <span class="operator">%</span> <span class="number">16</span>;
          color<span class="operator">.</span>setHsv((<span class="number">15</span> <span class="operator">-</span> index) <span class="operator">*</span> <span class="number">16</span><span class="operator">,</span> <span class="number">255</span><span class="operator">,</span> <span class="number">191</span>);
          painter<span class="operator">.</span>setPen(color);
          painter<span class="operator">.</span>drawText(x<span class="operator">,</span> y <span class="operator">-</span> ((sineTable<span class="operator">[</span>index<span class="operator">]</span> <span class="operator">*</span> metrics<span class="operator">.</span>height()) <span class="operator">/</span> <span class="number">400</span>)<span class="operator">,</span>
                           <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(text<span class="operator">[</span>i<span class="operator">]</span>));
          x <span class="operator">+</span><span class="operator">=</span> metrics<span class="operator">.</span>width(text<span class="operator">[</span>i<span class="operator">]</span>);
      }
  }

</pre>
<p>Each time the <code>paintEvent()</code> function is called, we create a <a href="../qtgui/qpainter.html">QPainter</a> object <code>painter</code> to draw the contents of the widget. For each character in <code>text</code>, we determine the color and the position on the wiggly line based on <code>step</code>. In addition, <code>x</code> is incremented by the character's width.</p>
<p>For simplicity, we assume that <a href="../qtgui/qfontmetrics.html#width-2">QFontMetrics::width</a>(<code>text</code>) returns the sum of the individual character widths (<a href="../qtgui/qfontmetrics.html#width-2">QFontMetrics::width</a>(<code>text[i]</code>)). In practice, this is not always the case because <a href="../qtgui/qfontmetrics.html#width-2">QFontMetrics::width</a>(<code>text</code>) also takes into account the kerning between certain letters (e.g&#x2e;, 'A' and 'V'). The result is that the text isn't perfectly centered. You can verify this by typing &quot;AVAVAVAVAVAV&quot; in the line edit.</p>
<pre class="cpp">

  <span class="type">void</span> WigglyWidget<span class="operator">::</span>timerEvent(<span class="type"><a href="../qtcore/qtimerevent.html">QTimerEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>timerId() <span class="operator">=</span><span class="operator">=</span> timer<span class="operator">.</span>timerId()) {
          <span class="operator">+</span><span class="operator">+</span>step;
          update();
      } <span class="keyword">else</span> {
          <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>timerEvent(event);
      }

</pre>
<p>The <code>timerEvent()</code> function receives all the timer events that are generated for this widget. If a timer event is sent from the widget's <a href="../qtcore/qbasictimer.html">QBasicTimer</a>, we increment <code>step</code> to make the text move, and call <a href="qwidget.html#update">QWidget::update</a>() to refresh the display. Any other timer event is passed on to the base class's implementation of the <a href="../qtcore/qobject.html#timerEvent">timerEvent()</a> function.</p>
<p>The <a href="qwidget.html#update">QWidget::update</a>() slot does not cause an immediate repaint; instead the slot schedules a paint event for processing when Qt returns to the main event loop. The paint events are then handled by <code>WigglyWidget</code>'s <code>paintEvent()</code> function.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-widgets-wiggly-dialog-cpp.html">widgets/wiggly/dialog.cpp</a></li>
<li><a href="qtwidgets-widgets-wiggly-dialog-h.html">widgets/wiggly/dialog.h</a></li>
<li><a href="qtwidgets-widgets-wiggly-wigglywidget-cpp.html">widgets/wiggly/wigglywidget.cpp</a></li>
<li><a href="qtwidgets-widgets-wiggly-wigglywidget-h.html">widgets/wiggly/wigglywidget.h</a></li>
<li><a href="qtwidgets-widgets-wiggly-main-cpp.html">widgets/wiggly/main.cpp</a></li>
<li><a href="qtwidgets-widgets-wiggly-wiggly-pro.html">widgets/wiggly/wiggly.pro</a></li>
</ul>
</div>
<!-- @@@widgets/wiggly -->
        </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>