Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 4535

qtbase5-doc-5.9.4-1.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" />
<!-- shapedclock.qdoc -->
  <title>Shaped Clock 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 >Shaped Clock 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="#shapedclock-class-definition">ShapedClock Class Definition</a></li>
<li class="level1"><a href="#shapedclock-class-implementation">ShapedClock Class Implementation</a></li>
<li class="level1"><a href="#notes-on-widget-masks">Notes on Widget Masks</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Shaped Clock Example</h1>
<span class="subtitle"></span>
<!-- $$$widgets/shapedclock-description -->
<div class="descr"> <a name="details"></a>
<div class="border"><p class="centerAlign"><img src="images/shapedclock-example.png" alt="" /></p></div><p>Widget masks are used to customize the shapes of top-level widgets by restricting the available area for painting. On some window systems, setting certain window flags will cause the window decoration (title bar, window frame, buttons) to be disabled, allowing specially-shaped windows to be created. In this example, we use this feature to create a circular window containing an analog clock.</p>
<p>Since this example's window does not provide a <b>File</b> menu or a close button, we provide a context menu with an <b>Exit</b> entry so that the example can be closed. Click the right mouse button over the window to open this menu.</p>
<a name="shapedclock-class-definition"></a>
<h2 id="shapedclock-class-definition">ShapedClock Class Definition</h2>
<p>The <code>ShapedClock</code> class is based on the <code>AnalogClock</code> class defined in the <a href="qtwidgets-widgets-analogclock-example.html">Analog Clock</a> example. The whole class definition is presented below:</p>
<pre class="cpp">

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

  <span class="keyword">public</span>:
      ShapedClock(<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="type"><a href="../qtcore/qsize.html">QSize</a></span> sizeHint() <span class="keyword">const</span> override;

  <span class="keyword">protected</span>:
      <span class="type">void</span> mouseMoveEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event) override;
      <span class="type">void</span> mousePressEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event) override;
      <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> resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>event) override;

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> dragPosition;
  };

</pre>
<p>The <a href="qwidget.html#paintEvent">paintEvent()</a> implementation is the same as that found in the <code>AnalogClock</code> class. We implement <a href="qwidget.html#sizeHint-prop">sizeHint()</a> so that we don't have to resize the widget explicitly. We also provide an event handler for resize events. This allows us to update the mask if the clock is resized.</p>
<p>Since the window containing the clock widget will have no title bar, we provide implementations for <a href="qwidget.html#mouseMoveEvent">mouseMoveEvent()</a> and <a href="qwidget.html#mousePressEvent">mousePressEvent()</a> to allow the clock to be dragged around the screen. The <code>dragPosition</code> variable lets us keep track of where the user last clicked on the widget.</p>
<a name="shapedclock-class-implementation"></a>
<h2 id="shapedclock-class-implementation">ShapedClock Class Implementation</h2>
<p>The <code>ShapedClock</code> constructor performs many of the same tasks as the <code>AnalogClock</code> constructor. We set up a timer and connect it to the widget's update() slot:</p>
<pre class="cpp">

  ShapedClock<span class="operator">::</span>ShapedClock(<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<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>FramelessWindowHint <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowSystemMenuHint)
  {
      <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span> <span class="operator">*</span>timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span>(<span class="keyword">this</span>);
      connect(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(update()));
      timer<span class="operator">-</span><span class="operator">&gt;</span>start(<span class="number">1000</span>);

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>quitAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      quitAction<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+Q&quot;</span>));
      connect(quitAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));
      addAction(quitAction);

      setContextMenuPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ActionsContextMenu);
      setToolTip(tr(<span class="string">&quot;Drag the clock with the left mouse button.\n&quot;</span>
                    <span class="string">&quot;Use the right mouse button to open a context menu.&quot;</span>));
      setWindowTitle(tr(<span class="string">&quot;Shaped Analog Clock&quot;</span>));
  }

</pre>
<p>We inform the window manager that the widget is not to be decorated with a window frame by setting the <a href="../qtcore/qt.html#WindowType-enum">Qt::FramelessWindowHint</a> flag on the widget. As a result, we need to provide a way for the user to move the clock around the screen.</p>
<p>Mouse button events are delivered to the <code>mousePressEvent()</code> handler:</p>
<pre class="cpp">

  <span class="type">void</span> ShapedClock<span class="operator">::</span>mousePressEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
          dragPosition <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>globalPos() <span class="operator">-</span> frameGeometry()<span class="operator">.</span>topLeft();
          event<span class="operator">-</span><span class="operator">&gt;</span>accept();
      }
  }

</pre>
<p>If the left mouse button is pressed over the widget, we record the displacement in global (screen) coordinates between the top-left position of the widget's frame (even when hidden) and the point where the mouse click occurred. This displacement will be used if the user moves the mouse while holding down the left button. Since we acted on the event, we accept it by calling its <a href="../qtcore/qevent.html#accept">accept()</a> function.</p>
<p class="centerAlign"><img src="images/shapedclock-dragging.png" alt="" /></p><p>The <code>mouseMoveEvent()</code> handler is called if the mouse is moved over the widget.</p>
<pre class="cpp">

  <span class="type">void</span> ShapedClock<span class="operator">::</span>mouseMoveEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>buttons() <span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
          move(event<span class="operator">-</span><span class="operator">&gt;</span>globalPos() <span class="operator">-</span> dragPosition);
          event<span class="operator">-</span><span class="operator">&gt;</span>accept();
      }
  }

</pre>
<p>If the left button is held down while the mouse is moved, the top-left corner of the widget is moved to the point given by subtracting the <code>dragPosition</code> from the current cursor position in global coordinates. If we drag the widget, we also accept the event.</p>
<p>The <code>paintEvent()</code> function is given for completeness. See the <a href="qtwidgets-widgets-analogclock-example.html">Analog Clock</a> example for a description of the process used to render the clock.</p>
<pre class="cpp">

  <span class="type">void</span> ShapedClock<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>)
  {
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> hourHand<span class="operator">[</span><span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="operator">-</span><span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">40</span>)
      };
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> minuteHand<span class="operator">[</span><span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="operator">-</span><span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">70</span>)
      };

      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> hourColor(<span class="number">127</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">127</span>);
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> minuteColor(<span class="number">0</span><span class="operator">,</span> <span class="number">127</span><span class="operator">,</span> <span class="number">127</span><span class="operator">,</span> <span class="number">191</span>);

      <span class="type">int</span> side <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(width()<span class="operator">,</span> height());
      <span class="type"><a href="../qtcore/qtime.html">QTime</a></span> time <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>currentTime();

      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
      painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);
      painter<span class="operator">.</span>translate(width() <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> height() <span class="operator">/</span> <span class="number">2</span>);
      painter<span class="operator">.</span>scale(side <span class="operator">/</span> <span class="number">200.0</span><span class="operator">,</span> side <span class="operator">/</span> <span class="number">200.0</span>);

      painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen);
      painter<span class="operator">.</span>setBrush(hourColor);

      painter<span class="operator">.</span>save();
      painter<span class="operator">.</span>rotate(<span class="number">30.0</span> <span class="operator">*</span> ((time<span class="operator">.</span>hour() <span class="operator">+</span> time<span class="operator">.</span>minute() <span class="operator">/</span> <span class="number">60.0</span>)));
      painter<span class="operator">.</span>drawConvexPolygon(hourHand<span class="operator">,</span> <span class="number">3</span>);
      painter<span class="operator">.</span>restore();

      painter<span class="operator">.</span>setPen(hourColor);

      <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> <span class="number">12</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          painter<span class="operator">.</span>drawLine(<span class="number">88</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">96</span><span class="operator">,</span> <span class="number">0</span>);
          painter<span class="operator">.</span>rotate(<span class="number">30.0</span>);
      }

      painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen);
      painter<span class="operator">.</span>setBrush(minuteColor);

      painter<span class="operator">.</span>save();
      painter<span class="operator">.</span>rotate(<span class="number">6.0</span> <span class="operator">*</span> (time<span class="operator">.</span>minute() <span class="operator">+</span> time<span class="operator">.</span>second() <span class="operator">/</span> <span class="number">60.0</span>));
      painter<span class="operator">.</span>drawConvexPolygon(minuteHand<span class="operator">,</span> <span class="number">3</span>);
      painter<span class="operator">.</span>restore();

      painter<span class="operator">.</span>setPen(minuteColor);

      <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> <span class="number">60</span>; <span class="operator">+</span><span class="operator">+</span>j) {
          <span class="keyword">if</span> ((j <span class="operator">%</span> <span class="number">5</span>) <span class="operator">!</span><span class="operator">=</span> <span class="number">0</span>)
              painter<span class="operator">.</span>drawLine(<span class="number">92</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">96</span><span class="operator">,</span> <span class="number">0</span>);
          painter<span class="operator">.</span>rotate(<span class="number">6.0</span>);
      }
  }

</pre>
<p>In the <code>resizeEvent()</code> handler, we re-use some of the code from the <code>paintEvent()</code> to determine the region of the widget that is visible to the user:</p>
<pre class="cpp">

  <span class="type">void</span> ShapedClock<span class="operator">::</span>resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span> <span class="comment">/* event */</span>)
  {
      <span class="type">int</span> side <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(width()<span class="operator">,</span> height());
      <span class="type"><a href="../qtgui/qregion.html">QRegion</a></span> maskedRegion(width() <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> side <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> height() <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> side <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> side<span class="operator">,</span>
                           side<span class="operator">,</span> <span class="type"><a href="../qtgui/qregion.html">QRegion</a></span><span class="operator">::</span>Ellipse);
      setMask(maskedRegion);
  }

</pre>
<p>Since the clock face is a circle drawn in the center of the widget, this is the region we use as the mask.</p>
<p>Although the lack of a window frame may make it difficult for the user to resize the widget on some platforms, it will not necessarily be impossible. The <code>resizeEvent()</code> function ensures that the widget mask will always be updated if the widget's dimensions change, and additionally ensures that it will be set up correctly when the widget is first displayed.</p>
<p>Finally, we implement the <code>sizeHint()</code> for the widget so that it is given a reasonable default size when it is first shown:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> ShapedClock<span class="operator">::</span>sizeHint() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
  }

</pre>
<a name="notes-on-widget-masks"></a>
<h2 id="notes-on-widget-masks">Notes on Widget Masks</h2>
<p>Since <a href="../qtgui/qregion.html">QRegion</a> allows arbitrarily complex regions to be created, widget masks can be made to suit the most unconventionally-shaped windows, and even allow widgets to be displayed with holes in them.</p>
<p>Widget masks can also be constructed by using the contents of pixmap to define the opaque part of the widget. For a pixmap with an alpha channel, a suitable mask can be obtained with <a href="../qtgui/qpixmap.html#mask">QPixmap::mask</a>().</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-widgets-shapedclock-shapedclock-cpp.html">widgets/shapedclock/shapedclock.cpp</a></li>
<li><a href="qtwidgets-widgets-shapedclock-shapedclock-h.html">widgets/shapedclock/shapedclock.h</a></li>
<li><a href="qtwidgets-widgets-shapedclock-main-cpp.html">widgets/shapedclock/main.cpp</a></li>
<li><a href="qtwidgets-widgets-shapedclock-shapedclock-pro.html">widgets/shapedclock/shapedclock.pro</a></li>
</ul>
</div>
<!-- @@@widgets/shapedclock -->
        </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>