Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 3925

qt-doc-4.8.5-10.fc18.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" />
<!-- flowlayout.qdoc -->
  <title>Qt 4.8: Flow Layout 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>Flow Layout 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="#flowlayout-class-definition">FlowLayout Class Definition</a></li>
<li class="level1"><a href="#flowlayout-class-implementation">FlowLayout Class Implementation</a></li>
</ul>
</div>
<h1 class="title">Flow Layout Example</h1>
<span class="subtitle"></span>
<!-- $$$layouts/flowlayout-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="layouts-flowlayout-flowlayout-cpp.html">layouts/flowlayout/flowlayout.cpp</a></li>
<li><a href="layouts-flowlayout-flowlayout-h.html">layouts/flowlayout/flowlayout.h</a></li>
<li><a href="layouts-flowlayout-window-cpp.html">layouts/flowlayout/window.cpp</a></li>
<li><a href="layouts-flowlayout-window-h.html">layouts/flowlayout/window.h</a></li>
<li><a href="layouts-flowlayout-main-cpp.html">layouts/flowlayout/main.cpp</a></li>
<li><a href="layouts-flowlayout-flowlayout-pro.html">layouts/flowlayout/flowlayout.pro</a></li>
</ul>
<p>The Flow Layout example demonstrates a custom layout that arranges child widgets from left to right and top to bottom in a top-level widget.<p class="centerAlign"><img src="images/flowlayout-example.png" alt="Screenshot of the Flow Layout example" /></p><p>The items are first laid out horizontally and then vertically when each line in the layout runs out of space.</p>
<p>The Flowlayout class mainly uses <a href="qlayout.html">QLayout</a> and <a href="qwidgetitem.html">QWidgetItem</a>, while the Window uses <a href="qwidget.html">QWidget</a> and <a href="qlabel.html">QLabel</a>. We will only document the definition and implementation of <tt>FlowLayout</tt> below.</p>
<a name="flowlayout-class-definition"></a>
<h2>FlowLayout Class Definition</h2>
<p>The <tt>FlowLayout</tt> class inherits <a href="qlayout.html">QLayout</a>. It is a custom layout class that arranges its child widgets horizontally and vertically.</p>
<pre class="cpp"> <span class="keyword">class</span> FlowLayout : <span class="keyword">public</span> <span class="type"><a href="qlayout.html">QLayout</a></span>
 {
 <span class="keyword">public</span>:
     FlowLayout(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span> <span class="type">int</span> margin <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="type">int</span> hSpacing <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="type">int</span> vSpacing <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>);
     FlowLayout(<span class="type">int</span> margin <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="type">int</span> hSpacing <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="type">int</span> vSpacing <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>);
     <span class="operator">~</span>FlowLayout();

     <span class="type">void</span> addItem(<span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>item);
     <span class="type">int</span> horizontalSpacing() <span class="keyword">const</span>;
     <span class="type">int</span> verticalSpacing() <span class="keyword">const</span>;
     <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Orientations expandingDirections() <span class="keyword">const</span>;
     <span class="type">bool</span> hasHeightForWidth() <span class="keyword">const</span>;
     <span class="type">int</span> heightForWidth(<span class="type">int</span>) <span class="keyword">const</span>;
     <span class="type">int</span> count() <span class="keyword">const</span>;
     <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>itemAt(<span class="type">int</span> index) <span class="keyword">const</span>;
     <span class="type"><a href="qsize.html">QSize</a></span> minimumSize() <span class="keyword">const</span>;
     <span class="type">void</span> setGeometry(<span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect);
     <span class="type"><a href="qsize.html">QSize</a></span> sizeHint() <span class="keyword">const</span>;
     <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>takeAt(<span class="type">int</span> index);

 <span class="keyword">private</span>:
     <span class="type">int</span> doLayout(<span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect<span class="operator">,</span> <span class="type">bool</span> testOnly) <span class="keyword">const</span>;
     <span class="type">int</span> smartSpacing(<span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>PixelMetric pm) <span class="keyword">const</span>;

     <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span><span class="operator">&gt;</span> itemList;
     <span class="type">int</span> m_hSpace;
     <span class="type">int</span> m_vSpace;
 };</pre>
<p>We reimplement functions inherited from <a href="qlayout.html">QLayout</a>. These functions add items to the layout and handle their orientation and geometry.</p>
<p>We also declare two private methods, <tt>doLayout()</tt> and <tt>smartSpacing()</tt>. <tt>doLayout()</tt> lays out the layout items, while the <tt>smartSpacing()</tt> function calculates the spacing between them.</p>
<a name="flowlayout-class-implementation"></a>
<h2>FlowLayout Class Implementation</h2>
<p>We start off by looking at the constructor:</p>
<pre class="cpp"> FlowLayout<span class="operator">::</span>FlowLayout(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span> <span class="type">int</span> margin<span class="operator">,</span> <span class="type">int</span> hSpacing<span class="operator">,</span> <span class="type">int</span> vSpacing)
     : <span class="type"><a href="qlayout.html">QLayout</a></span>(parent)<span class="operator">,</span> m_hSpace(hSpacing)<span class="operator">,</span> m_vSpace(vSpacing)
 {
     setContentsMargins(margin<span class="operator">,</span> margin<span class="operator">,</span> margin<span class="operator">,</span> margin);
 }

 FlowLayout<span class="operator">::</span>FlowLayout(<span class="type">int</span> margin<span class="operator">,</span> <span class="type">int</span> hSpacing<span class="operator">,</span> <span class="type">int</span> vSpacing)
     : m_hSpace(hSpacing)<span class="operator">,</span> m_vSpace(vSpacing)
 {
     setContentsMargins(margin<span class="operator">,</span> margin<span class="operator">,</span> margin<span class="operator">,</span> margin);
 }</pre>
<p>In the constructor we call <tt>setContentsMargins()</tt> to set the left, top, right and bottom margin. By default, <a href="qlayout.html">QLayout</a> uses values provided by the current style (see <a href="qstyle.html#PixelMetric-enum">QStyle::PixelMetric</a>).</p>
<pre class="cpp"> FlowLayout<span class="operator">::</span><span class="operator">~</span>FlowLayout()
 {
     <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>item;
     <span class="keyword">while</span> ((item <span class="operator">=</span> takeAt(<span class="number">0</span>)))
         <span class="keyword">delete</span> item;
 }</pre>
<p>In this example we reimplement <tt>addItem()</tt>, which is a pure virtual function. When using <tt>addItem()</tt> the ownership of the layout items is transferred to the layout, and it is therefore the layout's responsibility to delete them.</p>
<pre class="cpp"> <span class="type">void</span> FlowLayout<span class="operator">::</span>addItem(<span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>item)
 {
     itemList<span class="operator">.</span>append(item);
 }</pre>
<p><tt>addItem()</tt> is implemented to add items to the layout.</p>
<pre class="cpp"> <span class="type">int</span> FlowLayout<span class="operator">::</span>horizontalSpacing() <span class="keyword">const</span>
 {
     <span class="keyword">if</span> (m_hSpace <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
         <span class="keyword">return</span> m_hSpace;
     } <span class="keyword">else</span> {
         <span class="keyword">return</span> smartSpacing(<span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>PM_LayoutHorizontalSpacing);
     }
 }

 <span class="type">int</span> FlowLayout<span class="operator">::</span>verticalSpacing() <span class="keyword">const</span>
 {
     <span class="keyword">if</span> (m_vSpace <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
         <span class="keyword">return</span> m_vSpace;
     } <span class="keyword">else</span> {
         <span class="keyword">return</span> smartSpacing(<span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>PM_LayoutVerticalSpacing);
     }
 }</pre>
<p>We implement <tt>horizontalSpacing()</tt> and <tt>verticalSpacing()</tt> to get hold of the spacing between the widgets inside the layout. If the value is less than or equal to 0, this value will be used. If not, <tt>smartSpacing()</tt> will be called to calculate the spacing.</p>
<pre class="cpp"> <span class="type">int</span> FlowLayout<span class="operator">::</span>count() <span class="keyword">const</span>
 {
     <span class="keyword">return</span> itemList<span class="operator">.</span>size();
 }

 <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>FlowLayout<span class="operator">::</span>itemAt(<span class="type">int</span> index) <span class="keyword">const</span>
 {
     <span class="keyword">return</span> itemList<span class="operator">.</span>value(index);
 }

 <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>FlowLayout<span class="operator">::</span>takeAt(<span class="type">int</span> index)
 {
     <span class="keyword">if</span> (index <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span> <span class="operator">&amp;</span><span class="operator">&amp;</span> index <span class="operator">&lt;</span> itemList<span class="operator">.</span>size())
         <span class="keyword">return</span> itemList<span class="operator">.</span>takeAt(index);
     <span class="keyword">else</span>
         <span class="keyword">return</span> <span class="number">0</span>;
 }</pre>
<p>We then implement <tt>count()</tt> to return the number of items in the layout. To navigate the list of items we use <tt>itemAt()</tt> and takeAt() to remove and return items from the list. If an item is removed, the remaining items will be renumbered. All three functions are pure virtual functions from <a href="qlayout.html">QLayout</a>.</p>
<pre class="cpp"> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Orientations FlowLayout<span class="operator">::</span>expandingDirections() <span class="keyword">const</span>
 {
     <span class="keyword">return</span> <span class="number">0</span>;
 }</pre>
<p><tt>expandingDirections()</tt> returns the <a href="qt.html#Orientation-enum">Qt::Orientation</a>s in which the layout can make use of more space than its <tt>sizeHint()</tt>.</p>
<pre class="cpp"> <span class="type">bool</span> FlowLayout<span class="operator">::</span>hasHeightForWidth() <span class="keyword">const</span>
 {
     <span class="keyword">return</span> <span class="keyword">true</span>;
 }

 <span class="type">int</span> FlowLayout<span class="operator">::</span>heightForWidth(<span class="type">int</span> width) <span class="keyword">const</span>
 {
     <span class="type">int</span> height <span class="operator">=</span> doLayout(<span class="type"><a href="qrect.html">QRect</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> <span class="number">0</span>)<span class="operator">,</span> <span class="keyword">true</span>);
     <span class="keyword">return</span> height;
 }</pre>
<p>To adjust to widgets of which height is dependent on width, we implement <tt>heightForWidth()</tt>. The function <tt>hasHeightForWidth()</tt> is used to test for this dependency, and <tt>heightForWidth()</tt> passes the width on to <tt>doLayout()</tt> which in turn uses the width as an argument for the layout rect, i.e&#x2e;, the bounds in which the items are laid out. This rect does not include the layout margin().</p>
<pre class="cpp"> <span class="type">void</span> FlowLayout<span class="operator">::</span>setGeometry(<span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect)
 {
     <span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>setGeometry(rect);
     doLayout(rect<span class="operator">,</span> <span class="keyword">false</span>);
 }

 <span class="type"><a href="qsize.html">QSize</a></span> FlowLayout<span class="operator">::</span>sizeHint() <span class="keyword">const</span>
 {
     <span class="keyword">return</span> minimumSize();
 }

 <span class="type"><a href="qsize.html">QSize</a></span> FlowLayout<span class="operator">::</span>minimumSize() <span class="keyword">const</span>
 {
     <span class="type"><a href="qsize.html">QSize</a></span> size;
     <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>item;
     foreach (item<span class="operator">,</span> itemList)
         size <span class="operator">=</span> size<span class="operator">.</span>expandedTo(item<span class="operator">-</span><span class="operator">&gt;</span>minimumSize());

     size <span class="operator">+</span><span class="operator">=</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">2</span><span class="operator">*</span>margin()<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>margin());
     <span class="keyword">return</span> size;
 }</pre>
<p><tt>setGeometry()</tt> is normally used to do the actual layout, i.e&#x2e;, calculate the geometry of the layout's items. In this example, it calls <tt>doLayout()</tt> and passes the layout rect.</p>
<p><tt>sizeHint()</tt> returns the preferred size of the layout and <tt>minimumSize()</tt> returns the minimum size of the layout.</p>
<pre class="cpp"> <span class="type">int</span> FlowLayout<span class="operator">::</span>doLayout(<span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&amp;</span>rect<span class="operator">,</span> <span class="type">bool</span> testOnly) <span class="keyword">const</span>
 {
     <span class="type">int</span> left<span class="operator">,</span> top<span class="operator">,</span> right<span class="operator">,</span> bottom;
     getContentsMargins(<span class="operator">&amp;</span>left<span class="operator">,</span> <span class="operator">&amp;</span>top<span class="operator">,</span> <span class="operator">&amp;</span>right<span class="operator">,</span> <span class="operator">&amp;</span>bottom);
     <span class="type"><a href="qrect.html">QRect</a></span> effectiveRect <span class="operator">=</span> rect<span class="operator">.</span>adjusted(<span class="operator">+</span>left<span class="operator">,</span> <span class="operator">+</span>top<span class="operator">,</span> <span class="operator">-</span>right<span class="operator">,</span> <span class="operator">-</span>bottom);
     <span class="type">int</span> x <span class="operator">=</span> effectiveRect<span class="operator">.</span>x();
     <span class="type">int</span> y <span class="operator">=</span> effectiveRect<span class="operator">.</span>y();
     <span class="type">int</span> lineHeight <span class="operator">=</span> <span class="number">0</span>;</pre>
<p><tt>doLayout()</tt> handles the layout if <tt>horizontalSpacing()</tt> or <tt>verticalSpacing()</tt> don't return the default value. It uses <tt>getContentsMargins()</tt> to calculate the area available to the layout items.</p>
<pre class="cpp">     <span class="type"><a href="qlayoutitem.html">QLayoutItem</a></span> <span class="operator">*</span>item;
     foreach (item<span class="operator">,</span> itemList) {
         <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>wid <span class="operator">=</span> item<span class="operator">-</span><span class="operator">&gt;</span>widget();
         <span class="type">int</span> spaceX <span class="operator">=</span> horizontalSpacing();
         <span class="keyword">if</span> (spaceX <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>)
             spaceX <span class="operator">=</span> wid<span class="operator">-</span><span class="operator">&gt;</span>style()<span class="operator">-</span><span class="operator">&gt;</span>layoutSpacing(
                 <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>PushButton<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>PushButton<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Horizontal);
         <span class="type">int</span> spaceY <span class="operator">=</span> verticalSpacing();
         <span class="keyword">if</span> (spaceY <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>)
             spaceY <span class="operator">=</span> wid<span class="operator">-</span><span class="operator">&gt;</span>style()<span class="operator">-</span><span class="operator">&gt;</span>layoutSpacing(
                 <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>PushButton<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>PushButton<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Vertical);</pre>
<p>It then sets the proper amount of spacing for each widget in the layout, based on the current style.</p>
<pre class="cpp">         <span class="type">int</span> nextX <span class="operator">=</span> x <span class="operator">+</span> item<span class="operator">-</span><span class="operator">&gt;</span>sizeHint()<span class="operator">.</span>width() <span class="operator">+</span> spaceX;
         <span class="keyword">if</span> (nextX <span class="operator">-</span> spaceX <span class="operator">&gt;</span> effectiveRect<span class="operator">.</span>right() <span class="operator">&amp;</span><span class="operator">&amp;</span> lineHeight <span class="operator">&gt;</span> <span class="number">0</span>) {
             x <span class="operator">=</span> effectiveRect<span class="operator">.</span>x();
             y <span class="operator">=</span> y <span class="operator">+</span> lineHeight <span class="operator">+</span> spaceY;
             nextX <span class="operator">=</span> x <span class="operator">+</span> item<span class="operator">-</span><span class="operator">&gt;</span>sizeHint()<span class="operator">.</span>width() <span class="operator">+</span> spaceX;
             lineHeight <span class="operator">=</span> <span class="number">0</span>;
         }

         <span class="keyword">if</span> (<span class="operator">!</span>testOnly)
             item<span class="operator">-</span><span class="operator">&gt;</span>setGeometry(<span class="type"><a href="qrect.html">QRect</a></span>(<span class="type"><a href="qpoint.html">QPoint</a></span>(x<span class="operator">,</span> y)<span class="operator">,</span> item<span class="operator">-</span><span class="operator">&gt;</span>sizeHint()));

         x <span class="operator">=</span> nextX;
         lineHeight <span class="operator">=</span> <a href="qtglobal.html#qMax">qMax</a>(lineHeight<span class="operator">,</span> item<span class="operator">-</span><span class="operator">&gt;</span>sizeHint()<span class="operator">.</span>height());
     }
     <span class="keyword">return</span> y <span class="operator">+</span> lineHeight <span class="operator">-</span> rect<span class="operator">.</span>y() <span class="operator">+</span> bottom;
 }</pre>
<p>The position of each item in the layout is then calculated by adding the items width and the line height to the initial x and y coordinates. This in turn lets us find out whether the next item will fit on the current line or if it must be moved down to the next. We also find the height of the current line based on the widgets height.</p>
<pre class="cpp"> <span class="type">int</span> FlowLayout<span class="operator">::</span>smartSpacing(<span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>PixelMetric pm) <span class="keyword">const</span>
 {
     <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>parent();
     <span class="keyword">if</span> (<span class="operator">!</span>parent) {
         <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
     } <span class="keyword">else</span> <span class="keyword">if</span> (parent<span class="operator">-</span><span class="operator">&gt;</span>isWidgetType()) {
         <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>pw <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(parent);
         <span class="keyword">return</span> pw<span class="operator">-</span><span class="operator">&gt;</span>style()<span class="operator">-</span><span class="operator">&gt;</span>pixelMetric(pm<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> pw);
     } <span class="keyword">else</span> {
         <span class="keyword">return</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qlayout.html">QLayout</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(parent)<span class="operator">-</span><span class="operator">&gt;</span>spacing();
     }
 }</pre>
<p><tt>smartSpacing()</tt> is designed to get the default spacing for either the top-level layouts or the sublayouts. The default spacing for top-level layouts, when the parent is a <a href="qwidget.html">QWidget</a>, will be determined by querying the style. The default spacing for sublayouts, when the parent is a <a href="qlayout.html">QLayout</a>, will be determined by querying the spacing of the parent layout.</p>
</div>
<!-- @@@layouts/flowlayout -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. 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>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      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>