1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
<?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" />
<!-- basicgraphicslayouts.qdoc -->
<title>Qt 4.8: Basic Graphics Layouts Example</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<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>Basic Graphics Layouts Example</li>
</ul>
</div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#layoutitem-class-definition">LayoutItem Class Definition</a></li>
<li class="level1"><a href="#layoutitem-class-implementation">LayoutItem Class Implementation</a></li>
</ul>
</div>
<h1 class="title">Basic Graphics Layouts Example</h1>
<span class="subtitle"></span>
<!-- $$$graphicsview/basicgraphicslayouts-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="graphicsview-basicgraphicslayouts-layoutitem-cpp.html">graphicsview/basicgraphicslayouts/layoutitem.cpp</a></li>
<li><a href="graphicsview-basicgraphicslayouts-layoutitem-h.html">graphicsview/basicgraphicslayouts/layoutitem.h</a></li>
<li><a href="graphicsview-basicgraphicslayouts-window-cpp.html">graphicsview/basicgraphicslayouts/window.cpp</a></li>
<li><a href="graphicsview-basicgraphicslayouts-window-h.html">graphicsview/basicgraphicslayouts/window.h</a></li>
<li><a href="graphicsview-basicgraphicslayouts-main-cpp.html">graphicsview/basicgraphicslayouts/main.cpp</a></li>
<li><a href="graphicsview-basicgraphicslayouts-basicgraphicslayouts-pro.html">graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro</a></li>
<li><a href="graphicsview-basicgraphicslayouts-basicgraphicslayouts-qrc.html">graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/graphicsview/basicgraphicslayouts/images/block.png">graphicsview/basicgraphicslayouts/images/block.png</a></li>
</ul>
<p>The Basic Graphics Layouts example shows how to use the layout classes in <a href="qgraphicsview.html">QGraphicsView</a>: <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a> and <a href="qgraphicsgridlayout.html">QGraphicsGridLayout</a>. In addition to that it shows how to write your own custom layout item.<p class="centerAlign"><img src="images/basicgraphicslayouts-example.png" alt="Screenshot of the Basic Layouts Example" /></p><a name="window-class-definition"></a>
<h2>Window Class Definition</h2>
<p>The <tt>Window</tt> class is a subclass of <a href="qgraphicswidget.html">QGraphicsWidget</a>. It has a constructor with a <a href="qgraphicswidget.html">QGraphicsWidget</a> <i>parent</i> as its parameter.</p>
<pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qgraphicswidget.html">QGraphicsWidget</a></span> {
Q_OBJECT
<span class="keyword">public</span>:
Window(<span class="type"><a href="qgraphicswidget.html">QGraphicsWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
};</pre>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>The constructor of <tt>Window</tt> instantiates a <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a> object, <tt>windowLayout</tt>, with vertical orientation. We instantiate another <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a> object, <tt>linear</tt>, whose parent is <tt>windowLayout</tt>. Next, we create a <tt>LayoutItem</tt> object, <tt>item</tt> and add it to <tt>linear</tt> with the <a href="qgraphicslinearlayout.html#addItem">addItem()</a> function. We also provide <tt>item</tt> with a <a href="qgraphicslinearlayout.html#setStretchFactor">stretchFactor</a>.</p>
<pre class="cpp"> <span class="type"><a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a></span> <span class="operator">*</span>windowLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Vertical);
<span class="type"><a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a></span> <span class="operator">*</span>linear <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a></span>(windowLayout);
LayoutItem <span class="operator">*</span>item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
linear<span class="operator">-</span><span class="operator">></span>addItem(item);
linear<span class="operator">-</span><span class="operator">></span>setStretchFactor(item<span class="operator">,</span> <span class="number">1</span>);</pre>
<p>We repeat the process:</p>
<ul>
<li>create a new <tt>LayoutItem</tt>,</li>
<li>add the item <tt>linear</tt>, and</li>
<li>provide a stretch factor.</li>
</ul>
<pre class="cpp"> item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
linear<span class="operator">-</span><span class="operator">></span>addItem(item);
linear<span class="operator">-</span><span class="operator">></span>setStretchFactor(item<span class="operator">,</span> <span class="number">3</span>);
windowLayout<span class="operator">-</span><span class="operator">></span>addItem(linear);</pre>
<p>We then add <tt>linear</tt> to <tt>windowLayout</tt>, nesting two <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a> objects. Apart from the <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a>, we also use a <a href="qgraphicsgridlayout.html">QGraphicsGridLayout</a> object, <tt>grid</tt>, which is a 4x3 grid with some cells spanning to other rows.</p>
<p>We create seven <tt>LayoutItem</tt> objects and place them into <tt>grid</tt> with the <a href="qgraphicsgridlayout.html#addItem">addItem()</a> function as shown in the code snippet below:</p>
<pre class="cpp"> <span class="type"><a href="qgraphicsgridlayout.html">QGraphicsGridLayout</a></span> <span class="operator">*</span>grid <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsgridlayout.html">QGraphicsGridLayout</a></span>(windowLayout);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">1</span>);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
item<span class="operator">-</span><span class="operator">></span>setMaximumHeight(item<span class="operator">-</span><span class="operator">></span>minimumHeight());
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignVCenter);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
item<span class="operator">-</span><span class="operator">></span>setMaximumHeight(item<span class="operator">-</span><span class="operator">></span>minimumHeight());
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignVCenter);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">2</span>);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">2</span>);
item <span class="operator">=</span> <span class="keyword">new</span> LayoutItem;
grid<span class="operator">-</span><span class="operator">></span>addItem(item<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">2</span>);
windowLayout<span class="operator">-</span><span class="operator">></span>addItem(grid);</pre>
<p>The first item we add to <tt>grid</tt> is placed in the top left cell, spanning four rows. The next two items are placed in the second column, and they span two rows. Each item's <a href="qgraphicslayoutitem.html#maximumHeight">maximumHeight()</a> and <a href="qgraphicslayoutitem.html#minimumHeight">minimumHeight()</a> are set to be equal so that they do not expand vertically. As a result, these items will not fit vertically in their cells. So, we specify that they should be vertically aligned in the center of the cell using <a href="qt.html#AlignmentFlag-enum">Qt::AlignVCenter</a>.</p>
<p>Finally, <tt>grid</tt> itself is added to <tt>windowLayout</tt>. Unlike <a href="qgridlayout.html#addItem">QGridLayout::addItem</a>(), <a href="qgraphicsgridlayout.html#addItem">QGraphicsGridLayout::addItem</a>() requires a row and a column for its argument, specifying which cell the item should be positioned in. Also, if the <tt>rowSpan</tt> and <tt>columnSpan</tt> arguments are omitted, they will default to 1.</p>
<p>Note that we do not specify a parent for each <tt>LayoutItem</tt> that we construct, as all these items will be added to <tt>windowLayout</tt>. When we add an item to a layout, it will be automatically reparented to the widget on which the layout is installed.</p>
<pre class="cpp"> setLayout(windowLayout);
setWindowTitle(tr(<span class="string">"Basic Graphics Layouts Example"</span>));</pre>
<p>Now that we have set up <tt>grid</tt> and added it to <tt>windowLayout</tt>, we install <tt>windowLayout</tt> onto the window object using <a href="qgraphicswidget.html#layout-prop">QGraphicsWidget::setLayout</a>() and we set the window title.</p>
<a name="layoutitem-class-definition"></a>
<h2>LayoutItem Class Definition</h2>
<p>The <tt>LayoutItem</tt> class is a subclass of <a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a> and <a href="qgraphicsitem.html">QGraphicsItem</a>. It has a constructor, a destructor, and some required reimplementations. Since it inherits <a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a> it must reimplement {<a href="qgraphicslayoutitem.html#setGeometry">QGraphicsLayoutItem::setGeometry</a>()}{setGeometry()} and {<a href="qgraphicslayoutitem.html#sizeHint">QGraphicsLayoutItem::sizeHint</a>()}{sizeHint()}. In addition to that it inherits <a href="qgraphicsitem.html">QGraphicsItem</a>, so it must reimplement {<a href="qgraphicsitem.html#boundingRect">QGraphicsItem::boundingRect</a>()}{boundingRect()} and {<a href="qgraphicsitem.html#paint">QGraphicsItem::paint</a>()}{paint()}.</p>
<pre class="cpp"> <span class="keyword">class</span> LayoutItem : <span class="keyword">public</span> <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span><span class="operator">,</span> <span class="keyword">public</span> <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span>
{
<span class="keyword">public</span>:
LayoutItem(<span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>LayoutItem();
<span class="comment">// Inherited from QGraphicsLayoutItem</span>
<span class="type">void</span> setGeometry(<span class="keyword">const</span> <span class="type"><a href="qrectf.html">QRectF</a></span> <span class="operator">&</span>geom);
<span class="type"><a href="qsizef.html">QSizeF</a></span> sizeHint(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>SizeHint which<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsizef.html">QSizeF</a></span> <span class="operator">&</span>constraint <span class="operator">=</span> <span class="type"><a href="qsizef.html">QSizeF</a></span>()) <span class="keyword">const</span>;
<span class="comment">// Inherited from QGraphicsItem</span>
<span class="type"><a href="qrectf.html">QRectF</a></span> boundingRect() <span class="keyword">const</span>;
<span class="type">void</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="qstyleoptiongraphicsitem.html">QStyleOptionGraphicsItem</a></span> <span class="operator">*</span>option<span class="operator">,</span>
<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="number">0</span>);
<span class="keyword">private</span>:
<span class="type"><a href="qpixmap.html">QPixmap</a></span> <span class="operator">*</span>m_pix;
};</pre>
<p>The <tt>LayoutItem</tt> class also has a private instance of <a href="qpixmap.html">QPixmap</a>, <tt>m_pix</tt>.</p>
<a name="layoutitem-class-implementation"></a>
<h2>LayoutItem Class Implementation</h2>
<p>In <tt>LayoutItem</tt>'s constructor, <tt>m_pix</tt> is instantiated and the <tt>block.png</tt> image is loaded into it.</p>
<pre class="cpp"> LayoutItem<span class="operator">::</span>LayoutItem(<span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span> <span class="operator">*</span>parent<span class="comment">/* = 0*/</span>)
: <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span>()<span class="operator">,</span> <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span>(parent)
{
m_pix <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpixmap.html">QPixmap</a></span>(QLatin1String(<span class="string">":/images/block.png"</span>));
setGraphicsItem(<span class="keyword">this</span>);
}</pre>
<p>We use the <a href="qtglobal.html#Q_UNUSED">Q_UNUSED</a>() macro to prevent the compiler from generating warnings regarding unused parameters.</p>
<pre class="cpp"> <span class="type">void</span> LayoutItem<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="qstyleoptiongraphicsitem.html">QStyleOptionGraphicsItem</a></span> <span class="operator">*</span>option<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="comment">/*= 0*/</span>)
{
Q_UNUSED(widget);
Q_UNUSED(option);
<span class="type"><a href="qrectf.html">QRectF</a></span> frame(<span class="type"><a href="qpointf.html">QPointF</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span>)<span class="operator">,</span> geometry()<span class="operator">.</span>size());
<span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> w <span class="operator">=</span> m_pix<span class="operator">-</span><span class="operator">></span>width();
<span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> h <span class="operator">=</span> m_pix<span class="operator">-</span><span class="operator">></span>height();
<span class="type"><a href="qgradient.html#QGradientStops-typedef">QGradientStops</a></span> stops;</pre>
<p>The idea behind the <tt>paint()</tt> function is to paint the background rect then paint a rect around the pixmap.</p>
<pre class="cpp"> <span class="comment">// paint a background rect (with gradient)</span>
<span class="type"><a href="qlineargradient.html">QLinearGradient</a></span> gradient(frame<span class="operator">.</span>topLeft()<span class="operator">,</span> frame<span class="operator">.</span>topLeft() <span class="operator">+</span> <span class="type"><a href="qpointf.html">QPointF</a></span>(<span class="number">200</span><span class="operator">,</span><span class="number">200</span>));
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(<span class="number">0.0</span><span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">60</span><span class="operator">,</span> <span class="number">60</span><span class="operator">,</span> <span class="number">60</span>));
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(frame<span class="operator">.</span>height()<span class="operator">/</span><span class="number">2</span><span class="operator">/</span>frame<span class="operator">.</span>height()<span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">102</span><span class="operator">,</span> <span class="number">176</span><span class="operator">,</span> <span class="number">54</span>));
<span class="comment">//stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195, 55));</span>
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(<span class="number">1.0</span><span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">215</span><span class="operator">,</span> <span class="number">215</span><span class="operator">,</span> <span class="number">215</span>));
gradient<span class="operator">.</span>setStops(stops);
painter<span class="operator">-</span><span class="operator">></span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(gradient));
painter<span class="operator">-</span><span class="operator">></span>drawRoundedRect(frame<span class="operator">,</span> <span class="number">10.0</span><span class="operator">,</span> <span class="number">10.0</span>);
<span class="comment">// paint a rect around the pixmap (with gradient)</span>
<span class="type"><a href="qpointf.html">QPointF</a></span> pixpos <span class="operator">=</span> frame<span class="operator">.</span>center() <span class="operator">-</span> (<span class="type"><a href="qpointf.html">QPointF</a></span>(w<span class="operator">,</span> h)<span class="operator">/</span><span class="number">2</span>);
<span class="type"><a href="qrectf.html">QRectF</a></span> innerFrame(pixpos<span class="operator">,</span> <span class="type"><a href="qsizef.html">QSizeF</a></span>(w<span class="operator">,</span> h));
innerFrame<span class="operator">.</span>adjust(<span class="operator">-</span><span class="number">4</span><span class="operator">,</span> <span class="operator">-</span><span class="number">4</span><span class="operator">,</span> <span class="operator">+</span><span class="number">4</span><span class="operator">,</span> <span class="operator">+</span><span class="number">4</span>);
gradient<span class="operator">.</span>setStart(innerFrame<span class="operator">.</span>topLeft());
gradient<span class="operator">.</span>setFinalStop(innerFrame<span class="operator">.</span>bottomRight());
stops<span class="operator">.</span>clear();
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(<span class="number">0.0</span><span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">215</span><span class="operator">,</span> <span class="number">255</span><span class="operator">,</span> <span class="number">200</span>));
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(<span class="number">0.5</span><span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">102</span><span class="operator">,</span> <span class="number">176</span><span class="operator">,</span> <span class="number">54</span>));
stops <span class="operator"><</span><span class="operator"><</span> <span class="type"><a href="qgradient.html#QGradientStop-typedef">QGradientStop</a></span>(<span class="number">1.0</span><span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>));
gradient<span class="operator">.</span>setStops(stops);
painter<span class="operator">-</span><span class="operator">></span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(gradient));
painter<span class="operator">-</span><span class="operator">></span>drawRoundedRect(innerFrame<span class="operator">,</span> <span class="number">10.0</span><span class="operator">,</span> <span class="number">10.0</span>);
painter<span class="operator">-</span><span class="operator">></span>drawPixmap(pixpos<span class="operator">,</span> <span class="operator">*</span>m_pix);
}</pre>
<p>The reimplementation of <a href="qgraphicsitem.html#boundingRect">boundingRect()</a> will set the top left corner at (0,0), and the size of it will be the size of the layout items <a href="qgraphicslayoutitem.html#geometry">geometry()</a>. This is the area that we paint within.</p>
<pre class="cpp"> <span class="type"><a href="qrectf.html">QRectF</a></span> LayoutItem<span class="operator">::</span>boundingRect() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="type"><a href="qpointf.html">QPointF</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span>)<span class="operator">,</span> geometry()<span class="operator">.</span>size());
}</pre>
<p>The reimplementation of <a href="qgraphicslayoutitem.html#setGeometry">setGeometry()</a> simply calls its baseclass implementation. However, since this will change the boundingRect we must also call <a href="qgraphicsitem.html#prepareGeometryChange">prepareGeometryChange()</a>. Finally, we move the item according to <tt>geom.topLeft()</tt>.</p>
<pre class="cpp"> <span class="type">void</span> LayoutItem<span class="operator">::</span>setGeometry(<span class="keyword">const</span> <span class="type"><a href="qrectf.html">QRectF</a></span> <span class="operator">&</span>geom)
{
prepareGeometryChange();
<span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span><span class="operator">::</span>setGeometry(geom);
setPos(geom<span class="operator">.</span>topLeft());
}</pre>
<p>Since we don't want the size of the item to be smaller than the pixmap, we must make sure that we return a size hint that is larger than <tt>m_pix</tt>. We also add some extra space around for borders that we will paint later. Alternatively, you could scale the pixmap to prevent the item from becoming smaller than the pixmap. The preferred size is the same as the minimum size hint, while we set maximum to be a large value</p>
<pre class="cpp"> <span class="type"><a href="qsizef.html">QSizeF</a></span> LayoutItem<span class="operator">::</span>sizeHint(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>SizeHint which<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsizef.html">QSizeF</a></span> <span class="operator">&</span>constraint) <span class="keyword">const</span>
{
<span class="keyword">switch</span> (which) {
<span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>MinimumSize:
<span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>PreferredSize:
<span class="comment">// Do not allow a size smaller than the pixmap with two frames around it.</span>
<span class="keyword">return</span> m_pix<span class="operator">-</span><span class="operator">></span>size() <span class="operator">+</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">12</span><span class="operator">,</span> <span class="number">12</span>);
<span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>MaximumSize:
<span class="keyword">return</span> <span class="type"><a href="qsizef.html">QSizeF</a></span>(<span class="number">1000</span><span class="operator">,</span><span class="number">1000</span>);
<span class="keyword">default</span>:
<span class="keyword">break</span>;
}
<span class="keyword">return</span> constraint;
}</pre>
</div>
<!-- @@@graphicsview/basicgraphicslayouts -->
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation 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>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
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>
</body>
</html>
|