Sophie

Sophie

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

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- widgets-tutorial.qdoc -->
  <title>Widgets Tutorial - Nested Layouts | Qt Widgets 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Widgets Tutorial - Nested Layouts</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#setting-up-the-model">Setting up the Model</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Widgets Tutorial - Nested Layouts</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/widgets/nestedlayouts-description -->
<div class="descr"> <a name="details"></a>
<p>Just as widgets can contain other widgets, layouts can be used to provide different levels of grouping for widgets. Here, we want to display a label alongside a line edit at the top of a window, above a table view showing the results of a query.</p>
<p>We achieve this by creating two layouts: <code>queryLayout</code> is a <a href="qhboxlayout.html">QHBoxLayout</a> that contains <a href="qlabel.html">QLabel</a> and <a href="qlineedit.html">QLineEdit</a> widgets placed side-by-side; <code>mainLayout</code> is a <a href="qvboxlayout.html">QVBoxLayout</a> that contains <code>queryLayout</code> and a <a href="qtableview.html">QTableView</a> arranged vertically.</p>
<div class="qt-code"><div class="table"><table class="generic">
 <tr valign="top" class="odd"><td ><pre class="cpp">

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

  <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);
      <span class="type"><a href="qwidget.html">QWidget</a></span> window;

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>queryLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(
          <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">&quot;nestedlayouts&quot;</span><span class="operator">,</span> <span class="string">&quot;Query:&quot;</span>));
      <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>queryEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>();
      <span class="type"><a href="qtableview.html">QTableView</a></span> <span class="operator">*</span>resultView <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtableview.html">QTableView</a></span>();

      <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>queryLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>();
      queryLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(queryLabel);
      queryLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(queryEdit);

      <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>();
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addLayout(queryLayout);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(resultView);
      window<span class="operator">.</span>setLayout(mainLayout);

      <span class="comment">// Set up the model and configure the view...</span>
      window<span class="operator">.</span>setWindowTitle(
          <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">&quot;nestedlayouts&quot;</span><span class="operator">,</span> <span class="string">&quot;Nested layouts&quot;</span>));
      window<span class="operator">.</span>show();
      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
</td><td ><img src="images/widgets-tutorial-nestedlayouts.png" alt="" /></td></tr>
</table></div>
</div><p>Note that we call the <code>mainLayout</code>'s <a href="qboxlayout.html#addLayout">addLayout()</a> function to insert the <code>queryLayout</code> above the <code>resultView</code> table.</p>
<p>We have omitted the code that sets up the model containing the data shown by the <a href="qtableview.html">QTableView</a> widget, <code>resultView</code>. For completeness, we show this below.</p>
<p>As well as <a href="qhboxlayout.html">QHBoxLayout</a> and <a href="qvboxlayout.html">QVBoxLayout</a>, Qt also provides <a href="qgridlayout.html">QGridLayout</a> and <a href="qformlayout.html">QFormLayout</a> classes to help with more complex user interfaces. These can be seen if you run Qt Designer.</p>
<a name="setting-up-the-model"></a>
<h2 id="setting-up-the-model">Setting up the Model</h2>
<p>In the code above, we did not show where the table's data came from because we wanted to concentrate on the use of layouts. Here, we see that the model holds a number of items corresponding to rows, each of which is set up to contain data for two columns.</p>
<pre class="cpp">

      <span class="type"><a href="../qtgui/qstandarditemmodel.html">QStandardItemModel</a></span> model;
      model<span class="operator">.</span>setHorizontalHeaderLabels(
          <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">&quot;nestedlayouts&quot;</span><span class="operator">,</span> <span class="string">&quot;Name&quot;</span>)
                        <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">&quot;nestedlayouts&quot;</span><span class="operator">,</span> <span class="string">&quot;Office&quot;</span>));

      <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span><span class="operator">&gt;</span> rows <span class="operator">=</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span><span class="operator">&gt;</span>()
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Verne Nilsen&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;123&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Carlos Tang&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;77&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Bronwyn Hawcroft&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;119&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Alessandro Hanssen&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;32&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Andrew John Bakken&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;54&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Vanessa Weatherley&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;85&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Rebecca Dickens&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;17&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;David Bradley&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;42&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Knut Walters&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;25&quot;</span>)
          <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Andrea Jones&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;34&quot;</span>);

      foreach (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> row<span class="operator">,</span> rows) {
          <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtgui/qstandarditem.html">QStandardItem</a></span> <span class="operator">*</span><span class="operator">&gt;</span> items;
          foreach (<span class="type"><a href="../qtcore/qstring.html">QString</a></span> text<span class="operator">,</span> row)
              items<span class="operator">.</span>append(<span class="keyword">new</span> <span class="type"><a href="../qtgui/qstandarditem.html">QStandardItem</a></span>(text));
          model<span class="operator">.</span>appendRow(items);
      }

      resultView<span class="operator">-</span><span class="operator">&gt;</span>setModel(<span class="operator">&amp;</span>model);
      resultView<span class="operator">-</span><span class="operator">&gt;</span>verticalHeader()<span class="operator">-</span><span class="operator">&gt;</span>hide();
      resultView<span class="operator">-</span><span class="operator">&gt;</span>horizontalHeader()<span class="operator">-</span><span class="operator">&gt;</span>setStretchLastSection(<span class="keyword">true</span>);

</pre>
<p>The use of models and views is covered in the <a href="examples-itemviews.html">Item Views Examples</a> and in the <a href="model-view-programming.html">Model/View Programming</a> overview.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-tutorials-widgets-nestedlayouts-main-cpp.html">tutorials/widgets/nestedlayouts/main.cpp</a></li>
<li><a href="qtwidgets-tutorials-widgets-nestedlayouts-nestedlayouts-pro.html">tutorials/widgets/nestedlayouts/nestedlayouts.pro</a></li>
</ul>
</div>
<!-- @@@tutorials/widgets/nestedlayouts -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>