<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- graphicsview-simpleanchorlayout.qdoc --> <title>Simple Anchor Layout Example | Qt Widgets 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Simple Anchor Layout Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Simple Anchor Layout Example</h1> <span class="subtitle"></span> <!-- $$$graphicsview/simpleanchorlayout-brief --> <p>Demonstrates anchor layout on a graphics view scene.</p> <!-- @@@graphicsview/simpleanchorlayout --> <!-- $$$graphicsview/simpleanchorlayout-description --> <div class="descr"> <a name="details"></a> <p>The Simple Anchor Layout example shows the basic use of the <a href="qgraphicsanchorlayout.html">QGraphicsAnchorLayout</a> class.</p> <p class="centerAlign"><img src="images/graphicssimpleanchorlayout-example.png" alt="" /></p><p>The example starts by creating a <a href="qgraphicsscene.html">QGraphicsScene</a> (<code>scene</code>), 3 widgets (<code>a</code>, <code>b</code>, and <code>c</code>), and a QGraphicsAnchorlayout (<code>layout</code>).</p> <pre class="cpp"> <span class="type"><a href="qgraphicsscene.html">QGraphicsScene</a></span> <span class="operator">*</span>scene <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsscene.html">QGraphicsScene</a></span>(); Widget <span class="operator">*</span>a <span class="operator">=</span> <span class="keyword">new</span> Widget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>blue<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white<span class="operator">,</span> <span class="string">"a"</span>); a<span class="operator">-</span><span class="operator">></span>setPreferredSize(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>); Widget <span class="operator">*</span>b <span class="operator">=</span> <span class="keyword">new</span> Widget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>green<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black<span class="operator">,</span> <span class="string">"b"</span>); b<span class="operator">-</span><span class="operator">></span>setPreferredSize(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>); Widget <span class="operator">*</span>c <span class="operator">=</span> <span class="keyword">new</span> Widget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>red<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black<span class="operator">,</span> <span class="string">"c"</span>); c<span class="operator">-</span><span class="operator">></span>setPreferredSize(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>); <span class="type"><a href="qgraphicsanchorlayout.html">QGraphicsAnchorLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsanchorlayout.html">QGraphicsAnchorLayout</a></span>(); </pre> <p>First it anchors the top left corner of item <code>a</code> to the top left corner of <code>layout</code>. This can be done in two steps:</p> <pre class="cpp"> layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop); layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft); </pre> <p>Or in one step:</p> <pre class="cpp"> layout<span class="operator">-</span><span class="operator">></span>addCornerAnchors(a<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopLeftCorner<span class="operator">,</span> layout<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopLeftCorner); </pre> <p>Then the right anchor of <code>a</code> is anchored to the left anchor of <code>b</code>, and the top of item <code>b</code> is anchored to the bottom of <code>a</code>.</p> <pre class="cpp"> layout<span class="operator">-</span><span class="operator">></span>addAnchor(b<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorLeft<span class="operator">,</span> a<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorRight); layout<span class="operator">-</span><span class="operator">></span>addAnchor(b<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorTop<span class="operator">,</span> a<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorBottom); </pre> <p>Place a third widget <code>c</code> under widget <code>b</code>:</p> <pre class="cpp"> layout<span class="operator">-</span><span class="operator">></span>addAnchor(b<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorBottom<span class="operator">,</span> c<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AnchorTop); </pre> <p>Items <code>b</code> and <code>c</code> are anchored to each other horizontally:</p> <pre class="cpp"> layout<span class="operator">-</span><span class="operator">></span>addAnchors(b<span class="operator">,</span> c<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal); </pre> <p>Item c is anchored to the bottom right point of <code>layout</code></p> <pre class="cpp"> layout<span class="operator">-</span><span class="operator">></span>addCornerAnchors(c<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomRightCorner<span class="operator">,</span> layout<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomRightCorner); </pre> <p>Finally, <a href="graphicsview.html#qgraphicswidget">QGraphicsWidget</a> <code>w</code> is displayed in <a href="qgraphicsview.html">QGraphicsView</a> <code>view</code>.</p> <pre class="cpp"> <span class="type"><a href="qgraphicswidget.html">QGraphicsWidget</a></span> <span class="operator">*</span>w <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicswidget.html">QGraphicsWidget</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Window <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>CustomizeWindowHint <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowTitleHint); w<span class="operator">-</span><span class="operator">></span>setPos(<span class="number">20</span><span class="operator">,</span> <span class="number">20</span>); w<span class="operator">-</span><span class="operator">></span>setMinimumSize(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>); w<span class="operator">-</span><span class="operator">></span>setPreferredSize(<span class="number">320</span><span class="operator">,</span> <span class="number">240</span>); w<span class="operator">-</span><span class="operator">></span>setLayout(layout); w<span class="operator">-</span><span class="operator">></span>setWindowTitle(<span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">"simpleanchorlayout"</span><span class="operator">,</span> <span class="string">"QGraphicsAnchorLayout in use"</span>)); scene<span class="operator">-</span><span class="operator">></span>addItem(w); <span class="type"><a href="qgraphicsview.html">QGraphicsView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsview.html">QGraphicsView</a></span>(); view<span class="operator">-</span><span class="operator">></span>setScene(scene); view<span class="operator">-</span><span class="operator">></span>setWindowTitle(<span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>translate(<span class="string">"simpleanchorlayout"</span><span class="operator">,</span> <span class="string">"Simple Anchor Layout"</span>)); view<span class="operator">-</span><span class="operator">></span>resize(<span class="number">360</span><span class="operator">,</span> <span class="number">320</span>); view<span class="operator">-</span><span class="operator">></span>show(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); </pre> <p>Files:</p> <ul> <li><a href="qtwidgets-graphicsview-simpleanchorlayout-main-cpp.html">graphicsview/simpleanchorlayout/main.cpp</a></li> <li><a href="qtwidgets-graphicsview-simpleanchorlayout-simpleanchorlayout-pro.html">graphicsview/simpleanchorlayout/simpleanchorlayout.pro</a></li> </ul> </div> <p><b>See also </b><a href="qtwidgets-graphicsview-anchorlayout-example.html">Anchor Layout Example</a>.</p> <!-- @@@graphicsview/simpleanchorlayout --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 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>