Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 1dd17e0d683ef79b4bb6872bbf359d7f > files > 7101

qt4-doc-4.8.7-26.2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- simpledecoration.qdoc -->
  <title>Qt 4.8: Simple Decoration Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Simple Decoration Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#styles-and-window-decorations">Styles and Window Decorations</a></li>
<li class="level1"><a href="#mydecoration-class-definition">MyDecoration Class Definition</a></li>
<li class="level1"><a href="#mydecoration-class-implementation">MyDecoration Class Implementation</a></li>
<li class="level2"><a href="#finding-regions">Finding Regions</a></li>
<li class="level2"><a href="#painting-the-decoration">Painting the Decoration</a></li>
<li class="level1"><a href="#using-the-decoration">Using the Decoration</a></li>
<li class="level1"><a href="#notes">Notes</a></li>
</ul>
</div>
<h1 class="title">Simple Decoration Example</h1>
<span class="subtitle"></span>
<!-- $$$qws/simpledecoration-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="qws-simpledecoration-analogclock-cpp.html">qws/simpledecoration/analogclock.cpp</a></li>
<li><a href="qws-simpledecoration-analogclock-h.html">qws/simpledecoration/analogclock.h</a></li>
<li><a href="qws-simpledecoration-mydecoration-cpp.html">qws/simpledecoration/mydecoration.cpp</a></li>
<li><a href="qws-simpledecoration-mydecoration-h.html">qws/simpledecoration/mydecoration.h</a></li>
<li><a href="qws-simpledecoration-main-cpp.html">qws/simpledecoration/main.cpp</a></li>
<li><a href="qws-simpledecoration-simpledecoration-pro.html">qws/simpledecoration/simpledecoration.pro</a></li>
</ul>
<p>The Simple Decoration example shows how to create a custom window decoration for embedded applications.<p class="centerAlign"><img src="images/embedded-simpledecoration-example.png" alt="" /></p><p>By default, Qt for Embedded Linux applications display windows with one of the standard window decorations provided by Qt which are perfectly suitable for many situations. Nonetheless, for certain applications and devices, it is necessary to provide custom window decorations.</p>
<p>In this document, we examine the fundamental features of custom window decorations, and create a simple decoration as an example.</p>
<a name="styles-and-window-decorations"></a>
<h2>Styles and Window Decorations</h2>
<p>On many platforms, the style used for the contents of a window (including scroll bars) and the style used for the window decorations (the title bar, window borders, close, maximize and other buttons) are handled differently. This is usually because each application is responsible for rendering the contents of its own windows and the window manager renders the window decorations.</p>
<p>Although the situation is not quite like this on Qt for Embedded Linux because <a href="qapplication.html">QApplication</a> automatically handles window decorations as well, there are still two style mechanisms at work: <a href="qstyle.html">QStyle</a> and its associated classes are responsible for rendering widgets and subclasses of <a href="qdecoration.html">QDecoration</a> are responsible for rendering window decorations.</p>
<p class="centerAlign"><img src="images/embedded-simpledecoration-example-styles.png" alt="" /></p><p>Three decorations are provided with Qt for Embedded Linux: <i>default</i> is a basic style, <i>windows</i> resembles the classic Windows look and feel, and <i>styled</i> uses the <a href="qstyle.html">QStyle</a> classes for <a href="qmdisubwindow.html">QMdiSubWindow</a> to draw window decorations. Of these, <i>styled</i> is the most useful if you want to impose a consistent look and feel, but the window decorations may be too large for some use cases.</p>
<p>If none of these built-in decorations are suitable, a custom style can easily be created and used. To do this, we simply need to create a subclass of <a href="qdecorationdefault.html">QDecorationDefault</a> and apply it to a <a href="qapplication.html">QApplication</a> instance in a running application.</p>
<a name="mydecoration-class-definition"></a>
<h2>MyDecoration Class Definition</h2>
<p>The <tt>MyDecoration</tt> class is a subclass of <a href="qdecorationdefault.html">QDecorationDefault</a>, a subclass of <a href="qdecoration.html">QDecoration</a> that provides reasonable default behavior for a decoration:</p>
<pre class="cpp"> <span class="keyword">class</span> MyDecoration : <span class="keyword">public</span> <span class="type"><a href="qdecorationdefault.html">QDecorationDefault</a></span>
 {
 <span class="keyword">public</span>:
     MyDecoration();
     <span class="type"><a href="qregion.html">QRegion</a></span> region(<span class="keyword">const</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>insideRect<span class="operator">,</span> <span class="type">int</span> decorationRegion);
     <span class="type">bool</span> paint(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget<span class="operator">,</span> <span class="type">int</span> decorationRegion<span class="operator">,</span> DecorationState state);

 <span class="keyword">private</span>:
     <span class="type">int</span> border;
     <span class="type">int</span> buttonHeight;
     <span class="type">int</span> buttonMargin;
     <span class="type">int</span> buttonWidth;
     <span class="type">int</span> titleHeight;
     <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span><span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowType<span class="operator">,</span> DecorationRegion<span class="operator">&gt;</span> buttonHintMap;
     <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span>DecorationRegion<span class="operator">,</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span><span class="operator">&gt;</span> normalButtonPixmaps;
     <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span>DecorationRegion<span class="operator">,</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span><span class="operator">&gt;</span> maximizedButtonPixmaps;
     <span class="type"><a href="qvector.html">QVector</a></span><span class="operator">&lt;</span><span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowType<span class="operator">&gt;</span> buttonHints;
     <span class="type"><a href="qvector.html">QVector</a></span><span class="operator">&lt;</span>DecorationRegion<span class="operator">&gt;</span> stateRegions;
 };</pre>
<p>We only need to implement a constructor and reimplement the <a href="qdecorationdefault.html#region">region()</a> and <a href="qdecorationdefault.html#paint">paint()</a> functions to provide our own custom appearance for window decorations.</p>
<p>To make things fairly general, we provide a number of private variables to hold parameters which control certain aspects of the decoration's appearance. We also define some data structures that we will use to relate buttons in the window decorations to regions.</p>
<a name="mydecoration-class-implementation"></a>
<h2>MyDecoration Class Implementation</h2>
<p>In the constructor of the <tt>MyDecoration</tt> class, we set up some default values for the decoration, specifying a thin window border, a title bar that is just taller than the buttons it will hold, and we create a list of buttons that we support:</p>
<pre class="cpp"> MyDecoration<span class="operator">::</span>MyDecoration()
     : <span class="type"><a href="qdecorationdefault.html">QDecorationDefault</a></span>()
 {
     border <span class="operator">=</span> <span class="number">4</span>;
     titleHeight <span class="operator">=</span> <span class="number">24</span>;
     buttonWidth <span class="operator">=</span> <span class="number">20</span>;
     buttonHeight <span class="operator">=</span> <span class="number">20</span>;
     buttonMargin <span class="operator">=</span> <span class="number">2</span>;
     buttonHints <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Window
                 <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowMaximizeButtonHint
                 <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowContextHelpButtonHint;</pre>
<p>We map each of these <a href="qt.html#WindowType-enum">Qt::WindowFlags</a> to <a href="qdecoration.html#DecorationRegion-enum">QDecoration::DecorationRegion</a> enum values to help with the implementation of the <a href="#finding-regions">region() function implementation</a>.</p>
<pre class="cpp"> buttonHintMap<span class="operator">[</span><span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Window<span class="operator">]</span> <span class="operator">=</span> Close;
 buttonHintMap<span class="operator">[</span><span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowMaximizeButtonHint<span class="operator">]</span> <span class="operator">=</span> Maximize;
 buttonHintMap<span class="operator">[</span><span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowContextHelpButtonHint<span class="operator">]</span> <span class="operator">=</span> Help;</pre>
<p>In this decoration, we implement the buttons used in the decoration as pixmaps. To help us relate regions of the window to these, we define mappings between each <a href="qdecoration.html#DecorationRegion-enum">DecorationRegion</a> and its corresponding pixmap for two situations: when a window is shown normally and when it has been maximized. This is purely for cosmetic purposes.</p>
<pre class="cpp"> normalButtonPixmaps<span class="operator">[</span>Close<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_close_button);
 normalButtonPixmaps<span class="operator">[</span>Maximize<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_maximize_button);
 normalButtonPixmaps<span class="operator">[</span>Normalize<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_normalize_button);
 normalButtonPixmaps<span class="operator">[</span>Help<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_help_button);

 maximizedButtonPixmaps<span class="operator">[</span>Close<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_close_button);
 maximizedButtonPixmaps<span class="operator">[</span>Maximize<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_normalize_button);
 maximizedButtonPixmaps<span class="operator">[</span>Normalize<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_normalize_button);
 maximizedButtonPixmaps<span class="operator">[</span>Help<span class="operator">]</span> <span class="operator">=</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(_help_button);</pre>
<p>We finish the constructor by defining the regions for buttons that we understand. This will be useful when we are asked to give regions for window decoration buttons.</p>
<pre class="cpp"> stateRegions <span class="operator">&lt;</span><span class="operator">&lt;</span> Close <span class="operator">&lt;</span><span class="operator">&lt;</span> Maximize <span class="operator">&lt;</span><span class="operator">&lt;</span> Help;
 }</pre>
<a name="finding-regions"></a>
<h3>Finding Regions</h3>
<p>Each decoration needs to be able to describe the regions used for parts of the window furniture, such as the close button, window borders and title bar. We reimplement the <a href="qdecorationdefault.html#region">region()</a> function to do this for our decoration. This function returns a <a href="qregion.html">QRegion</a> object that describes an arbitrarily-shaped region of the screen that can itself be made up of several distinct areas.</p>
<pre class="cpp"> <span class="type"><a href="qregion.html">QRegion</a></span> MyDecoration<span class="operator">::</span>region(<span class="keyword">const</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>insideRect<span class="operator">,</span>
                              <span class="type">int</span> decorationRegion)
 {</pre>
<p>The function is called for a given <i>widget</i>, occupying a region specified by <i>insideRect</i>, and is expected to return a region for the collection of <a href="qdecoration.html#DecorationRegion-enum">DecorationRegion</a> enum values supplied in the <i>decorationRegion</i> parameter.</p>
<p>We begin by figuring out how much space in the decoration we will need to allocate for buttons, and where to place them:</p>
<pre class="cpp"> <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span>DecorationRegion<span class="operator">,</span> <span class="type">int</span><span class="operator">&gt;</span> buttons;
 <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowFlags flags <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">&gt;</span>windowFlags();
 <span class="type">int</span> dx <span class="operator">=</span> <span class="operator">-</span>buttonMargin <span class="operator">-</span> buttonWidth;

 foreach (<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowType button<span class="operator">,</span> buttonHints) {
     <span class="keyword">if</span> (flags <span class="operator">&amp;</span> button) {
         <span class="type">int</span> x <span class="operator">=</span> (buttons<span class="operator">.</span>size() <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> dx;
         buttons<span class="operator">[</span>buttonHintMap<span class="operator">[</span>button<span class="operator">]</span><span class="operator">]</span> <span class="operator">=</span> x;
     }
 }</pre>
<p>In a more sophisticated implementation, we might test the <i>decorationRegion</i> supplied for regions related to buttons and the title bar, and only perform this space allocation if asked for regions related to these.</p>
<p>We also use the information about the area occupied by buttons to determine how large an area we can use for the window title:</p>
<pre class="cpp"> <span class="type">int</span> titleRightMargin <span class="operator">=</span> buttons<span class="operator">.</span>size() <span class="operator">*</span> dx;

 <span class="type"><a href="qrect.html">QRect</a></span> outsideRect(insideRect<span class="operator">.</span>left() <span class="operator">-</span> border<span class="operator">,</span>
                   insideRect<span class="operator">.</span>top() <span class="operator">-</span> titleHeight <span class="operator">-</span> border<span class="operator">,</span>
                   insideRect<span class="operator">.</span>width() <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> border<span class="operator">,</span>
                   insideRect<span class="operator">.</span>height() <span class="operator">+</span> titleHeight <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> border);</pre>
<p>With these basic calculations done, we can start to compose a region, first checking whether we have been asked for all of the window, and we return immediately if so.</p>
<pre class="cpp"> <span class="type"><a href="qregion.html">QRegion</a></span> region;

 <span class="keyword">if</span> (decorationRegion <span class="operator">=</span><span class="operator">=</span> All) {
     region <span class="operator">+</span><span class="operator">=</span> <span class="type"><a href="qregion.html">QRegion</a></span>(outsideRect) <span class="operator">-</span> <span class="type"><a href="qregion.html">QRegion</a></span>(insideRect);
     <span class="keyword">return</span> region;
 }</pre>
<p>We examine each decoration region in turn, adding the corresponding region to the <tt>region</tt> object created earlier. We take care to avoid &quot;off by one&quot; errors in the coordinate calculations.</p>
<pre class="cpp"> <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Title) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(border<span class="operator">,</span> border<span class="operator">,</span> <span class="operator">-</span>border<span class="operator">,</span> <span class="number">0</span>);
     rect<span class="operator">.</span>setHeight(titleHeight);

     <span class="comment">// Adjust the width to accommodate buttons.</span>
     rect<span class="operator">.</span>setWidth(<a href="qtglobal.html#qMax">qMax</a>(<span class="number">0</span><span class="operator">,</span> rect<span class="operator">.</span>width() <span class="operator">+</span> titleRightMargin));
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Top) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(border<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span>border<span class="operator">,</span> <span class="number">0</span>);
     rect<span class="operator">.</span>setHeight(border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Left) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(<span class="number">0</span><span class="operator">,</span> border<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span>border);
     rect<span class="operator">.</span>setWidth(border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Right) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(<span class="number">0</span><span class="operator">,</span> border<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span>border);
     rect<span class="operator">.</span>setLeft(rect<span class="operator">.</span>right() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Bottom) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(border<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span>border<span class="operator">,</span> <span class="number">0</span>);
     rect<span class="operator">.</span>setTop(rect<span class="operator">.</span>bottom() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> TopLeft) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect;
     rect<span class="operator">.</span>setWidth(border);
     rect<span class="operator">.</span>setHeight(border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> TopRight) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect;
     rect<span class="operator">.</span>setLeft(rect<span class="operator">.</span>right() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     rect<span class="operator">.</span>setHeight(border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> BottomLeft) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect;
     rect<span class="operator">.</span>setWidth(border);
     rect<span class="operator">.</span>setTop(rect<span class="operator">.</span>bottom() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> BottomRight) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect;
     rect<span class="operator">.</span>setLeft(rect<span class="operator">.</span>right() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     rect<span class="operator">.</span>setTop(rect<span class="operator">.</span>bottom() <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> border);
     region <span class="operator">+</span><span class="operator">=</span> rect;
 }</pre>
<p>Unlike the window borders and title bar, the regions occupied by buttons many of the window decorations do not occupy fixed places in the window. Instead, their locations depend on which other buttons are present. We only add regions for buttons we can handle (defined in the <tt>stateRegions</tt>) member variable, and only for those that are present (defined in the <tt>buttons</tt> hash).</p>
<pre class="cpp"> foreach (<span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>DecorationRegion testRegion<span class="operator">,</span> stateRegions) {
     <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> testRegion <span class="keyword">and</span> buttons<span class="operator">.</span>contains(testRegion)) {
         <span class="comment">// Inside the title rectangle</span>
         <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> outsideRect<span class="operator">.</span>adjusted(border<span class="operator">,</span> border<span class="operator">,</span> <span class="operator">-</span>border<span class="operator">,</span> <span class="number">0</span>);
         rect<span class="operator">.</span>setHeight(titleHeight);

         dx <span class="operator">=</span> buttons<span class="operator">[</span>testRegion<span class="operator">]</span>;
         rect<span class="operator">.</span>setLeft(rect<span class="operator">.</span>right() <span class="operator">+</span> <span class="number">1</span> <span class="operator">+</span> dx);
         rect<span class="operator">.</span>setWidth(buttonWidth <span class="operator">+</span> buttonMargin);
         region <span class="operator">+</span><span class="operator">=</span> rect;
     }
 }</pre>
<p>The fully composed region can then be returned:</p>
<pre class="cpp"> <span class="keyword">return</span> region;
 }</pre>
<p>The information returned by this function is used when the decoration is painted. Ideally, this function should be implemented to perform all the calculations necessary to place elements of the decoration; this makes the implementation of the <tt>paint()</tt> function much easier.</p>
<a name="painting-the-decoration"></a>
<h3>Painting the Decoration</h3>
<p>The <tt>paint()</tt> function is responsible for drawing each window element for a given widget. Information about the decoration region, its state and the widget itself is provided along with a <a href="qpainter.html">QPainter</a> object to use.</p>
<p>The first check we make is for a call with no regions:</p>
<pre class="cpp"> <span class="type">bool</span> MyDecoration<span class="operator">::</span>paint(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget<span class="operator">,</span>
                          <span class="type">int</span> decorationRegion<span class="operator">,</span> DecorationState state)
 {
     <span class="keyword">if</span> (decorationRegion <span class="operator">=</span><span class="operator">=</span> None)
         <span class="keyword">return</span> <span class="keyword">false</span>;</pre>
<p>We return false to indicate that we have not painted anything. If we paint something, we must return true so that the window can be composed, if necessary.</p>
<p>Just as with the <tt>region()</tt> function, we test the decoration region to determine which elements need to be drawn. If we paint anything, we set the <tt>handled</tt> variable to true so that we can return the correct value when we have finished.</p>
<pre class="cpp"> <span class="type">bool</span> handled <span class="operator">=</span> <span class="keyword">false</span>;

 <span class="type"><a href="qpalette.html">QPalette</a></span> palette <span class="operator">=</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>palette();
 <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span>DecorationRegion<span class="operator">,</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span><span class="operator">&gt;</span> buttonPixmaps;

 <span class="keyword">if</span> (widget<span class="operator">-</span><span class="operator">&gt;</span>windowState() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowMaximized)
     buttonPixmaps <span class="operator">=</span> maximizedButtonPixmaps;
 <span class="keyword">else</span>
     buttonPixmaps <span class="operator">=</span> normalButtonPixmaps;

 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Title) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> Title)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Base));
     painter<span class="operator">-</span><span class="operator">&gt;</span>save();
     painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qpen.html">QPen</a></span>(palette<span class="operator">.</span>color(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Text)));
     painter<span class="operator">-</span><span class="operator">&gt;</span>drawText(rect<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignCenter<span class="operator">,</span> widget<span class="operator">-</span><span class="operator">&gt;</span>windowTitle());
     painter<span class="operator">-</span><span class="operator">&gt;</span>restore();
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Top) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> Top)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Left) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> Left)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Right) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> Right)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> Bottom) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> Bottom)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> TopLeft) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> TopLeft)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> TopRight) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> TopRight)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> BottomLeft) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> BottomLeft)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }
 <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> BottomRight) {
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(widget<span class="operator">,</span> BottomRight)<span class="operator">.</span>boundingRect();
     painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Dark));
     handled <span class="operator">=</span> <span class="keyword">true</span>;
 }</pre>
<p>Note that we use our own <tt>region()</tt> implementation to determine where to draw decorations.</p>
<p>Since the <tt>region()</tt> function performs calculations to place buttons, we can simply test the window flags against the buttons we support (using the <tt>buttonHintMap</tt> defined in the constructor), and draw each button in the relevant region:</p>
<pre class="cpp"> <span class="type">int</span> margin <span class="operator">=</span> (titleHeight <span class="operator">-</span> <span class="number">16</span>) <span class="operator">/</span> <span class="number">2</span>;
 <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowFlags flags <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">&gt;</span>windowFlags();

 foreach (DecorationRegion testRegion<span class="operator">,</span> stateRegions) {
     <span class="keyword">if</span> (decorationRegion <span class="operator">&amp;</span> testRegion <span class="operator">&amp;</span><span class="operator">&amp;</span> flags <span class="operator">&amp;</span> buttonHintMap<span class="operator">.</span>key(testRegion)) {
         <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> <span class="type"><a href="qdecoration.html">QDecoration</a></span><span class="operator">::</span>region(
             widget<span class="operator">,</span> testRegion)<span class="operator">.</span>boundingRect();
         painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(rect<span class="operator">,</span> palette<span class="operator">.</span>brush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Base));

         <span class="type"><a href="qrect.html">QRect</a></span> buttonRect <span class="operator">=</span> rect<span class="operator">.</span>adjusted(<span class="number">0</span><span class="operator">,</span> margin<span class="operator">,</span> <span class="operator">-</span>buttonMargin <span class="operator">-</span> margin<span class="operator">,</span>
                                          <span class="operator">-</span>buttonMargin);
         painter<span class="operator">-</span><span class="operator">&gt;</span>drawPixmap(buttonRect<span class="operator">.</span>topLeft()<span class="operator">,</span> buttonPixmaps<span class="operator">[</span>testRegion<span class="operator">]</span>);
         handled <span class="operator">=</span> <span class="keyword">true</span>;
     }
 }</pre>
<p>Finally, we return the value of <tt>handled</tt> to indicate whether any painting was performed:</p>
<pre class="cpp"> <span class="keyword">return</span> handled;
 }</pre>
<p>We now have a decoration class that we can use in an application.</p>
<a name="using-the-decoration"></a>
<h2>Using the Decoration</h2>
<p>In the <tt>main.cpp</tt> file, we set up the application as usual, but we also create an instance of our decoration and set it as the standard decoration for the application:</p>
<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>argv<span class="operator">[</span><span class="operator">]</span>)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
     MyDecoration <span class="operator">*</span>decoration <span class="operator">=</span> <span class="keyword">new</span> MyDecoration();
     app<span class="operator">.</span>qwsSetDecoration(decoration);</pre>
<p>This causes all windows opened by this application to use our decoration. To demonstrate this, we show the analog clock widget from the <a href="widgets-analogclock.html">Analog Clock Example</a>, which we build into the application:</p>
<pre class="cpp">     AnalogClock clock;
     clock<span class="operator">.</span>show();

     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>The application can be run either <a href="qt-embedded-running.html">as a server or a client application</a>. In both cases, it will use our decoration rather than the default one provided with Qt.</p>
<a name="notes"></a>
<h2>Notes</h2>
<p>This example does not cache any information about the state or buttons used for each window. This means that the <tt>region()</tt> function calculates the locations and regions of buttons in cases where it could re-use existing information.</p>
<p>If you run the application as a window server, you may expect client applications to use our decoration in preference to the default Qt decoration. However, it is up to each application to draw its own decoration, so this will not happen automatically. One way to achieve this is to compile the decoration with each application that needs it; another way is to build the decoration as a plugin, using the <a href="qdecorationplugin.html">QDecorationPlugin</a> class, and load it into the server and client applications.</p>
</div>
<!-- @@@qws/simpledecoration -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
      Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      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.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      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. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>