Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 1902

qtbase5-doc-5.12.6-2.mga7.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" />
<!-- rasterwindow.qdoc -->
  <title>Raster Window Example | Qt GUI 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="qtgui-index.html">Qt GUI</a></td><td >Raster Window Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtgui-index.html">Qt 5.12.6 Reference Documentation</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="#application-entry-point">Application Entry Point</a></li>
<li class="level1"><a href="#rasterwindow-declaration">RasterWindow Declaration</a></li>
<li class="level1"><a href="#rasterwindow-implementation">RasterWindow Implementation</a></li>
<li class="level1"><a href="#rendering-asynchronously">Rendering Asynchronously</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Raster Window Example</h1>
<span class="subtitle"></span>
<!-- $$$rasterwindow-brief -->
<p>This example shows how to create a minimal <a href="qwindow.html">QWindow</a> based application using <a href="qpainter.html">QPainter</a> for rendering.</p>
<!-- @@@rasterwindow -->
<!-- $$$rasterwindow-description -->
<div class="descr"> <a name="details"></a>
<a name="application-entry-point"></a>
<h2 id="application-entry-point">Application Entry Point</h2>
<pre class="cpp">

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
  {
      <span class="type"><a href="qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);

      RasterWindow window;
      window<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>The entry point for a <a href="qwindow.html">QWindow</a> based application is the <a href="qguiapplication.html">QGuiApplication</a> class. It manages the GUI application's control flow and main settings. We pass the command line arguments which can be used to pick up certain system wide options.</p>
<p>From there, we go on to create our window instance and then call the <a href="qwindow.html#show">QWindow::show</a>() function to tell the windowing system that this window should now be made visible on screen.</p>
<p>Once this is done, we enter the application's event loop so the application can run.</p>
<a name="rasterwindow-declaration"></a>
<h2 id="rasterwindow-declaration">RasterWindow Declaration</h2>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QtGui&gt;</span>

  <span class="keyword">class</span> RasterWindow : <span class="keyword">public</span> <span class="type"><a href="qwindow.html">QWindow</a></span>
  {
      Q_OBJECT
  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> RasterWindow(<span class="type"><a href="qwindow.html">QWindow</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

      <span class="keyword">virtual</span> <span class="type">void</span> render(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter);

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> renderLater();
      <span class="type">void</span> renderNow();

  <span class="keyword">protected</span>:
      bool event(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>event) override;

      <span class="type">void</span> resizeEvent(<span class="type"><a href="qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>event) override;
      <span class="type">void</span> exposeEvent(<span class="type"><a href="qexposeevent.html">QExposeEvent</a></span> <span class="operator">*</span>event) override;

  <span class="keyword">private</span>:
      <span class="type"><a href="qbackingstore.html">QBackingStore</a></span> <span class="operator">*</span>m_backingStore;
  };

</pre>
<p>We first start by including the <code>&lt;QtGui&gt;</code> header. This means we can use all classes in the Qt GUI module. Classes can also be included individually if that is preferred.</p>
<p>The RasterWindow class subclasses <a href="qwindow.html">QWindow</a> directly and provides a constructor which allows the window to be a sub-window of another <a href="qwindow.html">QWindow</a>. Parent-less QWindows show up in the windowing system as top-level windows.</p>
<p>The class declares a <a href="qbackingstore.html">QBackingStore</a> which is what we use to manage the window's back buffer for <a href="qpainter.html">QPainter</a> based graphics.</p>
<p><i>The raster window is also reused in a few other examples and adds a few helper functions, like renderLater().</i></p>
<a name="rasterwindow-implementation"></a>
<h2 id="rasterwindow-implementation">RasterWindow Implementation</h2>
<pre class="cpp">

  RasterWindow<span class="operator">::</span>RasterWindow(<span class="type"><a href="qwindow.html">QWindow</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qwindow.html">QWindow</a></span>(parent)
      <span class="operator">,</span> m_backingStore(<span class="keyword">new</span> <span class="type"><a href="qbackingstore.html">QBackingStore</a></span>(<span class="keyword">this</span>))
  {
      setGeometry(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">300</span><span class="operator">,</span> <span class="number">200</span>);
  }

</pre>
<p>In the constructor we create the backingstore and pass it the window instance it is supposed to manage. We also set the initial window geometry.</p>
<pre class="cpp">

  <span class="type">void</span> RasterWindow<span class="operator">::</span>exposeEvent(<span class="type"><a href="qexposeevent.html">QExposeEvent</a></span> <span class="operator">*</span>)
  {
      <span class="keyword">if</span> (isExposed())
          renderNow();
  }

</pre>
<p>Shortly after calling <a href="qwindow.html#show">QWindow::show</a>() on a created window, the virtual function <a href="qwindow.html#exposeEvent">QWindow::exposeEvent</a>() will be called to notify us that the window's exposure in the windowing system has changed. The event contains the exposed sub-region, but since we will anyway draw the entire window every time, we do not make use of that.</p>
<p>The function <a href="qwindow.html#isExposed">QWindow::isExposed</a>() will tell us if the window is showing or not. We need this as the exposeEvent is called also when the window becomes obscured in the windowing system. If the window is showing, we call renderNow() to draw the window immediately. We want to draw right away so we can present the system with some visual content.</p>
<pre class="cpp">

  <span class="type">void</span> RasterWindow<span class="operator">::</span>resizeEvent(<span class="type"><a href="qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>resizeEvent)
  {
      m_backingStore<span class="operator">-</span><span class="operator">&gt;</span>resize(resizeEvent<span class="operator">-</span><span class="operator">&gt;</span>size());
  }

</pre>
<p>The resize event is guaranteed to be called prior to the window being shown on screen and will also be called whenever the window is resized while on screen. We use this to resize the back buffer, and defer rendering to the corresponding/following expose event.</p>
<pre class="cpp">

  <span class="type">void</span> RasterWindow<span class="operator">::</span>renderNow()
  {
      <span class="keyword">if</span> (<span class="operator">!</span>isExposed())
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> rect(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> width()<span class="operator">,</span> height());
      m_backingStore<span class="operator">-</span><span class="operator">&gt;</span>beginPaint(rect);

      <span class="type"><a href="qpaintdevice.html">QPaintDevice</a></span> <span class="operator">*</span>device <span class="operator">=</span> m_backingStore<span class="operator">-</span><span class="operator">&gt;</span>paintDevice();
      <span class="type"><a href="qpainter.html">QPainter</a></span> painter(device);

      painter<span class="operator">.</span>fillRect(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> width()<span class="operator">,</span> height()<span class="operator">,</span> <span class="type"><a href="qgradient.html">QGradient</a></span><span class="operator">::</span>NightFade);
      render(<span class="operator">&amp;</span>painter);
      painter<span class="operator">.</span>end();

      m_backingStore<span class="operator">-</span><span class="operator">&gt;</span>endPaint();
      m_backingStore<span class="operator">-</span><span class="operator">&gt;</span>flush(rect);
  }

</pre>
<p>The renderNow function sets up what is needed for a <a href="qwindow.html">QWindow</a> to render its content using <a href="qpainter.html">QPainter</a>. As obscured windows have will not be visible, we abort if the window is not exposed in the windowing system. This can for instance happen when another window fully obscures this window.</p>
<p>We start the drawing by calling <a href="qbackingstore.html#beginPaint">QBackingStore::beginPaint</a>() on the region we want to draw. Then we get the <a href="qpaintdevice.html">QPaintDevice</a> of the back buffer and create a <a href="qpainter.html">QPainter</a> to render to that paint device.</p>
<p>To void leaving traces from the previous rendering and start with a clean buffer, we fill the entire buffer with the color white. Then we call the virtual render() function which does the actual drawing of this window.</p>
<p>After drawing is complete, we call endPaint() to signal that we are done rendering and present the contents in the back buffer using <a href="qbackingstore.html#flush">QBackingStore::flush</a>().</p>
<pre class="cpp">

  <span class="type">void</span> RasterWindow<span class="operator">::</span>render(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter)
  {
      painter<span class="operator">-</span><span class="operator">&gt;</span>drawText(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> width()<span class="operator">,</span> height())<span class="operator">,</span> <span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>AlignCenter<span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;QWindow&quot;</span>));
  }

</pre>
<p>The render function contains the drawing code for the window. In this minial example, we only draw the string &quot;<a href="qwindow.html">QWindow</a>&quot; in the center.</p>
<a name="rendering-asynchronously"></a>
<h2 id="rendering-asynchronously">Rendering Asynchronously</h2>
<pre class="cpp">

  <span class="type">void</span> RasterWindow<span class="operator">::</span>renderLater()
  {
      requestUpdate();
  }

</pre>
<p>We went through a few places where the window needed to repainted immediately. There are some cases where this is not desirable, but rather let the application return to the event loop and schedule the repaint for later. We achieve this by requesting an update, using <a href="qwindow.html#requestUpdate">QWindow::requestUpdate</a>(), which will then be delivered when the system is ready to repaint.</p>
<pre class="cpp">

  bool RasterWindow<span class="operator">::</span>event(<span class="type"><a href="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>UpdateRequest) {
          renderNow();
          <span class="keyword">return</span> <span class="keyword">true</span>;
      }
      <span class="keyword">return</span> <span class="type"><a href="qwindow.html">QWindow</a></span><span class="operator">::</span>event(event);
  }

</pre>
<p>We reimplement the virtual <a href="../qtcore/qobject.html#event">QObject::event</a>() function to handle the update event. When the event comes in we call renderNow() to render the window right away.</p>
<p>Files:</p>
<ul>
<li><a href="qtgui-rasterwindow-main-cpp.html">rasterwindow/main.cpp</a></li>
<li><a href="qtgui-rasterwindow-rasterwindow-cpp.html">rasterwindow/rasterwindow.cpp</a></li>
<li><a href="qtgui-rasterwindow-rasterwindow-h.html">rasterwindow/rasterwindow.h</a></li>
<li><a href="qtgui-rasterwindow-rasterwindow-pro.html">rasterwindow/rasterwindow.pro</a></li>
</ul>
</div>
<!-- @@@rasterwindow -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>