<?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.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="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"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#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 <QtWidgets></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">"nestedlayouts"</span><span class="operator">,</span> <span class="string">"Query:"</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">></span>addWidget(queryLabel); queryLayout<span class="operator">-</span><span class="operator">></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">></span>addLayout(queryLayout); mainLayout<span class="operator">-</span><span class="operator">></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">"nestedlayouts"</span><span class="operator">,</span> <span class="string">"Nested layouts"</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"><</span><span class="operator"><</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">"nestedlayouts"</span><span class="operator">,</span> <span class="string">"Name"</span>) <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">"nestedlayouts"</span><span class="operator">,</span> <span class="string">"Office"</span>)); <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span><span class="operator">></span> rows <span class="operator">=</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Verne Nilsen"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"123"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Carlos Tang"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"77"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Bronwyn Hawcroft"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"119"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Alessandro Hanssen"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"32"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Andrew John Bakken"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"54"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Vanessa Weatherley"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"85"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Rebecca Dickens"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"17"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"David Bradley"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"42"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Knut Walters"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"25"</span>) <span class="operator"><</span><span class="operator"><</span> (<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Andrea Jones"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"34"</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"><</span><span class="type"><a href="../qtgui/qstandarditem.html">QStandardItem</a></span> <span class="operator">*</span><span class="operator">></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">></span>setModel(<span class="operator">&</span>model); resultView<span class="operator">-</span><span class="operator">></span>verticalHeader()<span class="operator">-</span><span class="operator">></span>hide(); resultView<span class="operator">-</span><span class="operator">></span>horizontalHeader()<span class="operator">-</span><span class="operator">></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">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>