source: trunk/doc/html/customlayout-example.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 34.2 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/customlayout/customlayout.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Customized Layoutmanager</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Customized Layoutmanager</h1>
33
34
35<p>
36This examples demonstrates how to write customized layout (geometry) managers
37like card layouts, border layout and flow layouts.
38<p> See also: <a href="layout.html">Documentation of Geometry Management</a>.
39<p> <hr>
40<p> Header file of the flow layout:
41<p> <pre>/****************************************************************************
42** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
43**
44** Definition of simple flow layout for custom layout example
45**
46** Created : 979899
47**
48** Copyright (C) 1997-2007 Trolltech ASA. All rights reserved.
49**
50** This file is part of an example program for Qt. This example
51** program may be used, distributed and modified without limitation.
52**
53*****************************************************************************/
54
55#ifndef FLOW_H
56#define FLOW_H
57
58#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
59#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;
60
61class SimpleFlow : public <a href="qlayout.html">QLayout</a>
62{
63public:
64 SimpleFlow( <a href="qwidget.html">QWidget</a> *parent, int border=0, int space=-1,
65 const char *name=0 )
66 : <a href="qlayout.html">QLayout</a>( parent, border, space, name ),
67 cached_width(0) {}
68 SimpleFlow( <a href="qlayout.html">QLayout</a>* parent, int space=-1, const char *name=0 )
69 : <a href="qlayout.html">QLayout</a>( parent, space, name ),
70 cached_width(0) {}
71 SimpleFlow( int space=-1, const char *name=0 )
72 : <a href="qlayout.html">QLayout</a>( space, name ),
73 cached_width(0) {}
74
75 ~SimpleFlow();
76
77 void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item);
78 bool hasHeightForWidth() const;
79 int heightForWidth( int ) const;
80 <a href="qsize.html">QSize</a> sizeHint() const;
81 <a href="qsize.html">QSize</a> minimumSize() const;
82 <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();
83 QSizePolicy::ExpandData expanding() const;
84
85protected:
86 void setGeometry( const <a href="qrect.html">QRect</a>&amp; );
87
88private:
89 int doLayout( const <a href="qrect.html">QRect</a>&amp;, bool testonly = FALSE );
90 <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; list;
91 int cached_width;
92 int cached_hfw;
93
94};
95
96#endif
97</pre>
98
99<p> <hr>
100<p> Implementation of the flow layout:
101<p> <pre>/****************************************************************************
102** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
103**
104** Implementing your own layout: flow example
105**
106** Copyright (C) 1996-2007 Trolltech ASA. All rights reserved.
107**
108** This file is part of an example program for Qt. This example
109** program may be used, distributed and modified without limitation.
110**
111*****************************************************************************/
112
113#include "flow.h"
114
115class SimpleFlowIterator :public <a href="qglayoutiterator.html">QGLayoutIterator</a>
116{
117public:
118 SimpleFlowIterator( <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *l ) :idx(0), list(l) {}
119 uint count() const;
120 <a href="qlayoutitem.html">QLayoutItem</a> *current();
121 <a href="qlayoutitem.html">QLayoutItem</a> *next();
122 <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();
123
124private:
125 int idx;
126 <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *list;
127
128};
129
130uint <a name="f452"></a>SimpleFlowIterator::count() const
131{
132<a name="x1479"></a> return list-&gt;<a href="qptrlist.html#count">count</a>();
133}
134
135<a name="x1464"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#current">current</a>()
136{
137<a name="x1478"></a> return idx &lt; int(count()) ? list-&gt;<a href="qptrlist.html#at">at</a>(idx) : 0;
138}
139
140<a name="x1465"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#next">next</a>()
141{
142 idx++; return current();
143}
144
145<a name="x1466"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
146{
147<a name="x1480"></a> return idx &lt; int(count()) ? list-&gt;<a href="qptrlist.html#take">take</a>( idx ) : 0;
148}
149
150SimpleFlow::~SimpleFlow()
151{
152 <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
153}
154
155
156<a name="x1473"></a>int SimpleFlow::<a href="qlayoutitem.html#heightForWidth">heightForWidth</a>( int w ) const
157{
158 if ( cached_width != w ) {
159 //Not all C++ compilers support "mutable" yet:
160 SimpleFlow * mthis = (SimpleFlow*)this;
161 int h = mthis-&gt;doLayout( QRect(0,0,w,0), TRUE );
162 mthis-&gt;cached_hfw = h;
163 mthis-&gt;cached_width = w;
164 return h;
165 }
166 return cached_hfw;
167}
168
169<a name="x1467"></a>void SimpleFlow::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item)
170{
171<a name="x1477"></a> list.<a href="qptrlist.html#append">append</a>( item );
172}
173
174<a name="x1472"></a>bool SimpleFlow::<a href="qlayoutitem.html#hasHeightForWidth">hasHeightForWidth</a>() const
175{
176 return TRUE;
177}
178
179<a name="x1476"></a>QSize SimpleFlow::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
180{
181 return minimumSize();
182}
183
184<a name="x1468"></a>QSizePolicy::ExpandData SimpleFlow::<a href="qlayout.html#expanding">expanding</a>() const
185{
186 return QSizePolicy::NoDirection;
187}
188
189<a name="x1469"></a>QLayoutIterator SimpleFlow::<a href="qlayout.html#iterator">iterator</a>()
190{
191 return QLayoutIterator( new SimpleFlowIterator( &amp;list ) );
192}
193
194<a name="x1471"></a>void SimpleFlow::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;r )
195{
196 QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( r );
197 doLayout( r );
198}
199
200int <a name="f451"></a>SimpleFlow::doLayout( const <a href="qrect.html">QRect</a> &amp;r, bool testonly )
201{
202 int x = r.<a href="qrect.html#x">x</a>();
203 int y = r.<a href="qrect.html#y">y</a>();
204 int h = 0; //height of this line so far.
205 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
206 <a href="qlayoutitem.html">QLayoutItem</a> *o;
207<a name="x1481"></a> while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
208 ++it;
209 int nextX = x + o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width() + spacing();
210<a name="x1482"></a> if ( nextX - spacing() &gt; r.<a href="qrect.html#right">right</a>() &amp;&amp; h &gt; 0 ) {
211 x = r.<a href="qrect.html#x">x</a>();
212 y = y + h + spacing();
213 nextX = x + o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width() + spacing();
214 h = 0;
215 }
216 if ( !testonly )
217<a name="x1475"></a> o-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( QPoint( x, y ), o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>() ) );
218 x = nextX;
219 h = QMAX( h, o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height() );
220 }
221 return y + h - r.<a href="qrect.html#y">y</a>();
222}
223
224<a name="x1470"></a>QSize SimpleFlow::<a href="qlayout.html#minimumSize">minimumSize</a>() const
225{
226 <a href="qsize.html">QSize</a> s(0,0);
227 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
228 <a href="qlayoutitem.html">QLayoutItem</a> *o;
229 while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
230 ++it;
231<a name="x1485"></a><a name="x1474"></a> s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
232 }
233 return s;
234}
235</pre>
236
237<p> <hr>
238<p> Header file of the border layout:
239<p> <pre>/****************************************************************************
240** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
241**
242** Definition of simple flow layout for custom layout example
243**
244** Created : 979899
245**
246** Copyright (C) 1997-2007 Trolltech ASA. All rights reserved.
247**
248** This file is part of an example program for Qt. This example
249** program may be used, distributed and modified without limitation.
250**
251*****************************************************************************/
252
253#ifndef BORDER_H
254#define BORDER_H
255
256#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
257#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;
258
259class BorderWidgetItem : public <a href="qwidgetitem.html">QWidgetItem</a>
260{
261public:
262 BorderWidgetItem( <a href="qwidget.html">QWidget</a> *w )
263 : <a href="qwidgetitem.html">QWidgetItem</a>( w )
264 {}
265
266 void setGeometry( const <a href="qrect.html">QRect</a> &amp;r )
267 { widget()-&gt;setGeometry( r ); }
268
269};
270
271class BorderLayout : public <a href="qlayout.html">QLayout</a>
272{
273public:
274 enum Position {
275 West = 0,
276 North,
277 South,
278 East,
279 Center
280 };
281
282 struct BorderLayoutStruct
283 {
284 BorderLayoutStruct( <a href="qlayoutitem.html">QLayoutItem</a> *i, Position p ) {
285 item = i;
286 pos = p;
287 }
288
289 <a href="qlayoutitem.html">QLayoutItem</a> *item;
290 Position pos;
291 };
292
293 enum SizeType {
294 Minimum = 0,
295 SizeHint
296 };
297
298 BorderLayout( <a href="qwidget.html">QWidget</a> *parent, int border = 0, int autoBorder = -1,
299 const char *name = 0 )
300 : <a href="qlayout.html">QLayout</a>( parent, border, autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ),
301 sizeDirty( TRUE ), msizeDirty( TRUE )
302 {}
303
304 BorderLayout( <a href="qlayout.html">QLayout</a>* parent, int autoBorder = -1, const char *name = 0 )
305 : <a href="qlayout.html">QLayout</a>( parent, autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ),
306 sizeDirty( TRUE ), msizeDirty( TRUE )
307 {}
308
309 BorderLayout( int autoBorder = -1, const char *name = 0 )
310 : <a href="qlayout.html">QLayout</a>( autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ),
311 sizeDirty( TRUE ), msizeDirty( TRUE )
312 {}
313
314 ~BorderLayout();
315
316 void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item );
317
318 void addWidget( <a href="qwidget.html">QWidget</a> *widget, Position pos );
319 void add( <a href="qlayoutitem.html">QLayoutItem</a> *item, Position pos );
320
321 bool hasHeightForWidth() const;
322
323 <a href="qsize.html">QSize</a> sizeHint() const;
324 <a href="qsize.html">QSize</a> minimumSize() const;
325
326 <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();
327
328 QSizePolicy::ExpandData expanding() const;
329
330protected:
331 void setGeometry( const <a href="qrect.html">QRect</a> &amp;rect );
332
333private:
334 void doLayout( const <a href="qrect.html">QRect</a> &amp;rect, bool testonly = FALSE );
335 void calcSize( SizeType st );
336
337 <a href="qptrlist.html">QPtrList</a>&lt;BorderLayoutStruct&gt; list;
338 <a href="qsize.html">QSize</a> cached, mcached;
339 bool sizeDirty, msizeDirty;
340
341};
342
343#endif
344</pre>
345
346<p> <hr>
347<p> Implementation of the border layout:
348<p> <pre>/****************************************************************************
349** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
350**
351** Implementing your own layout: flow example
352**
353** Copyright (C) 1996-2007 Trolltech ASA. All rights reserved.
354**
355** This file is part of an example program for Qt. This example
356** program may be used, distributed and modified without limitation.
357**
358*****************************************************************************/
359
360#include "border.h"
361
362class BorderLayoutIterator : public <a href="qglayoutiterator.html">QGLayoutIterator</a>
363{
364public:
365 BorderLayoutIterator( const <a href="qptrlist.html">QPtrList</a>&lt;BorderLayout::BorderLayoutStruct&gt; *l )
366 : idx( 0 ) , list( (QPtrList&lt;BorderLayout::BorderLayoutStruct&gt;*)l )
367 {}
368
369 uint count() const;
370 <a href="qlayoutitem.html">QLayoutItem</a> *current();
371 BorderLayout::BorderLayoutStruct *currentStruct();
372 void toFirst();
373 <a href="qlayoutitem.html">QLayoutItem</a> *next();
374 <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();
375 BorderLayoutIterator &amp;operator++();
376
377private:
378 int idx;
379 <a href="qptrlist.html">QPtrList</a>&lt;BorderLayout::BorderLayoutStruct&gt; *list;
380
381};
382
383uint <a name="f456"></a>BorderLayoutIterator::count() const
384{
385<a name="x1502"></a> return list-&gt;<a href="qptrlist.html#count">count</a>();
386}
387
388<a name="x1486"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#current">current</a>()
389{
390<a name="x1501"></a> return idx &lt; (int)count() ? list-&gt;<a href="qptrlist.html#at">at</a>( idx )-&gt;item : 0;
391}
392
393BorderLayout::BorderLayoutStruct *<a name="f457"></a>BorderLayoutIterator::currentStruct()
394{
395 return idx &lt; (int)count() ? list-&gt;<a href="qptrlist.html#at">at</a>( idx ) : 0;
396}
397
398void <a name="f458"></a>BorderLayoutIterator::toFirst()
399{
400 idx = 0;
401}
402
403<a name="x1487"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#next">next</a>()
404{
405 idx++;
406 return current();
407}
408
409<a name="x1488"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
410{
411 BorderLayout::BorderLayoutStruct *b
412<a name="x1503"></a> = idx &lt; int( list-&gt;<a href="qptrlist.html#count">count</a>() ) ? list-&gt;<a href="qptrlist.html#take">take</a>( idx ) : 0;
413 <a href="qlayoutitem.html">QLayoutItem</a> *item = b ? b-&gt;item : 0;
414 delete b;
415 return item;
416}
417
418BorderLayoutIterator &amp;BorderLayoutIterator::operator++()
419{
420 next();
421 return *this;
422}
423
424BorderLayout::~BorderLayout()
425{
426 <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
427}
428
429
430<a name="x1490"></a>void BorderLayout::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item )
431{
432 <a href="qlayout.html#add">add</a>( item, West );
433}
434
435void <a name="f453"></a>BorderLayout::addWidget( <a href="qwidget.html">QWidget</a> *widget, Position pos )
436{
437 <a href="qlayout.html#add">add</a>( new BorderWidgetItem( widget ), pos );
438}
439
440<a name="x1489"></a>void BorderLayout::<a href="qlayout.html#add">add</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item, Position pos )
441{
442<a name="x1500"></a> list.<a href="qptrlist.html#append">append</a>( new BorderLayoutStruct( item, pos ) );
443 sizeDirty = TRUE; msizeDirty = TRUE;
444 calcSize( SizeHint ); calcSize( Minimum );
445}
446
447<a name="x1496"></a>bool BorderLayout::<a href="qlayoutitem.html#hasHeightForWidth">hasHeightForWidth</a>() const
448{
449 return FALSE;
450}
451
452<a name="x1499"></a>QSize BorderLayout::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
453{
454 return cached;
455}
456
457<a name="x1493"></a>QSize BorderLayout::<a href="qlayout.html#minimumSize">minimumSize</a>() const
458{
459 return cached;
460}
461
462<a name="x1491"></a>QSizePolicy::ExpandData BorderLayout::<a href="qlayout.html#expanding">expanding</a>() const
463
464{
465 return QSizePolicy::BothDirections;
466}
467
468<a name="x1492"></a>QLayoutIterator BorderLayout::<a href="qlayout.html#iterator">iterator</a>()
469{
470 return QLayoutIterator( new BorderLayoutIterator( &amp;list ) );
471}
472
473<a name="x1494"></a>void BorderLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;rct )
474{
475 QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( rct );
476 doLayout( rct );
477}
478
479void <a name="f454"></a>BorderLayout::doLayout( const <a href="qrect.html">QRect</a> &amp;rct, bool /*testonly*/ )
480{
481 int ew = 0, ww = 0, nh = 0, sh = 0;
482 int h = 0;
483
484 BorderLayoutIterator it( &amp;list );
485 BorderLayoutStruct *o;
486 BorderLayoutStruct *center = 0;
487 while ( ( o = it.currentStruct() ) != 0 ) {
488 ++it;
489
490 if ( o-&gt;pos == North ) {
491<a name="x1506"></a><a name="x1505"></a><a name="x1498"></a> o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( rct.<a href="qrect.html#x">x</a>(), nh, rct.<a href="qrect.html#width">width</a>(), o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height() ) );
492<a name="x1495"></a> nh += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() + spacing();
493 }
494 if ( o-&gt;pos == South ) {
495 o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().x(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().y(),
496 rct.<a href="qrect.html#width">width</a>(), o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height() ) );
497 sh += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() + spacing();
498 o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( rct.<a href="qrect.html#x">x</a>(), rct.<a href="qrect.html#y">y</a>() + rct.<a href="qrect.html#height">height</a>() - sh + spacing(),
499 o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() ) );
500 }
501 if ( o-&gt;pos == Center )
502 center = o;
503 }
504
505 h = rct.<a href="qrect.html#height">height</a>() - nh - sh;
506
507 it.toFirst();
508 while ( ( o = it.currentStruct() ) != 0 ) {
509 ++it;
510
511 if ( o-&gt;pos == West ) {
512 o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( rct.<a href="qrect.html#x">x</a>() + ww, nh, o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width(), h ) );
513 ww += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width() + spacing();
514 }
515 if ( o-&gt;pos == East ) {
516 o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().x(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().y(),
517 o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width(), h ) );
518 ew += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width() + spacing();
519 o-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( rct.<a href="qrect.html#x">x</a>() + rct.<a href="qrect.html#width">width</a>() - ew + spacing(), nh,
520 o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() ) );
521 }
522 }
523
524 if ( center )
525 center-&gt;item-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( ww, nh, rct.<a href="qrect.html#width">width</a>() - ew - ww, h ) );
526}
527
528void <a name="f455"></a>BorderLayout::calcSize( SizeType st )
529{
530 if ( ( st == Minimum &amp;&amp; !msizeDirty ) ||
531 ( st == SizeHint &amp;&amp; !sizeDirty ) )
532 return;
533
534 int w = 0, h = 0;
535
536 BorderLayoutIterator it( &amp;list );
537 BorderLayoutStruct *o;
538 while ( ( o = it.currentStruct() ) != 0 ) {
539 ++it;
540 if ( o-&gt;pos == North ||
541 o-&gt;pos == South ) {
542 if ( st == Minimum )
543<a name="x1497"></a> h += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().height();
544 else
545 h += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height();
546 }
547 else if ( o-&gt;pos == West ||
548 o-&gt;pos == East ) {
549 if ( st == Minimum )
550 w += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().width();
551 else
552 w += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width();
553 } else {
554 if ( st == Minimum ) {
555 h += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().height();
556 w += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().width();
557 }
558 else {
559 h += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height();
560 w += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width();
561 }
562 }
563 }
564
565 if ( st == Minimum ) {
566 msizeDirty = FALSE;
567 mcached = QSize( w, h );
568 } else {
569 sizeDirty = FALSE;
570 cached = QSize( w, h );
571 }
572
573 return;
574}
575</pre>
576
577<p> <hr>
578<p> Header file of the card layout:
579<p> <pre>/****************************************************************************
580** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
581**
582** Definition of simple flow layout for custom layout example
583**
584** Created : 979899
585**
586** Copyright (C) 1997-2007 Trolltech ASA. All rights reserved.
587**
588** This file is part of an example program for Qt. This example
589** program may be used, distributed and modified without limitation.
590**
591*****************************************************************************/
592
593#ifndef CARD_H
594#define CARD_H
595
596#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
597#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;
598
599class CardLayout : public <a href="qlayout.html">QLayout</a>
600{
601public:
602 CardLayout( <a href="qwidget.html">QWidget</a> *parent, int dist )
603 : <a href="qlayout.html">QLayout</a>( parent, 0, dist ) {}
604 CardLayout( <a href="qlayout.html">QLayout</a>* parent, int dist)
605 : <a href="qlayout.html">QLayout</a>( parent, dist ) {}
606 CardLayout( int dist )
607 : <a href="qlayout.html">QLayout</a>( dist ) {}
608 ~CardLayout();
609
610 void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item );
611 <a href="qsize.html">QSize</a> sizeHint() const;
612 <a href="qsize.html">QSize</a> minimumSize() const;
613 <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();
614 void setGeometry( const <a href="qrect.html">QRect</a> &amp;rect );
615
616private:
617 <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; list;
618
619};
620
621#endif
622</pre>
623
624<p> <hr>
625<p> Implementation of the card layout:
626<p> <pre>/****************************************************************************
627** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
628**
629** Implementing your own layout: flow example
630**
631** Copyright (C) 1996-2007 Trolltech ASA. All rights reserved.
632**
633** This file is part of an example program for Qt. This example
634** program may be used, distributed and modified without limitation.
635**
636*****************************************************************************/
637
638#include "card.h"
639
640class CardLayoutIterator :public <a href="qglayoutiterator.html">QGLayoutIterator</a>
641{
642public:
643 CardLayoutIterator( <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *l )
644 : idx( 0 ), list( l ) {}
645
646 <a href="qlayoutitem.html">QLayoutItem</a> *current();
647 <a href="qlayoutitem.html">QLayoutItem</a> *next();
648 <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();
649
650private:
651 int idx;
652 <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *list;
653};
654
655<a name="x1508"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#current">current</a>()
656{
657<a name="x1520"></a><a name="x1519"></a> return idx &lt; int( list-&gt;<a href="qptrlist.html#count">count</a>() ) ? list-&gt;<a href="qptrlist.html#at">at</a>( idx ) : 0;
658}
659
660<a name="x1509"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#next">next</a>()
661{
662 idx++; return current();
663}
664
665<a name="x1510"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
666{
667<a name="x1521"></a> return idx &lt; int( list-&gt;<a href="qptrlist.html#count">count</a>() ) ?list-&gt;<a href="qptrlist.html#take">take</a>( idx ) : 0;
668}
669
670
671
672<a name="x1512"></a>QLayoutIterator CardLayout::<a href="qlayout.html#iterator">iterator</a>()
673{
674 return QLayoutIterator( new CardLayoutIterator( &amp;list ) );
675}
676
677CardLayout::~CardLayout()
678{
679 <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
680}
681
682<a name="x1511"></a>void CardLayout::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item )
683{
684<a name="x1518"></a> list.<a href="qptrlist.html#append">append</a>( item );
685}
686
687<a name="x1514"></a>void CardLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;rct )
688{
689 QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( rct );
690
691 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it( list );
692<a name="x1522"></a> if ( it.<a href="qptrlistiterator.html#count">count</a>() == 0 )
693 return;
694
695 <a href="qlayoutitem.html">QLayoutItem</a> *o;
696
697 int i = 0;
698
699 int w = rct.<a href="qrect.html#width">width</a>() - ( list.<a href="qptrlist.html#count">count</a>() - 1 ) * spacing();
700 int h = rct.<a href="qrect.html#height">height</a>() - ( list.<a href="qptrlist.html#count">count</a>() - 1 ) * spacing();
701
702<a name="x1523"></a> while ( ( o=it.<a href="qptrlistiterator.html#current">current</a>() ) != 0 ) {
703 ++it;
704 <a href="qrect.html">QRect</a> geom( rct.<a href="qrect.html#x">x</a>() + i * spacing(), rct.<a href="qrect.html#y">y</a>() + i * spacing(),
705 w, h );
706<a name="x1516"></a> o-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( geom );
707 ++i;
708 }
709}
710
711<a name="x1517"></a>QSize CardLayout::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
712{
713 <a href="qsize.html">QSize</a> s(0,0);
714 int n = list.<a href="qptrlist.html#count">count</a>();
715 if ( n &gt; 0 )
716 s = QSize(100,70); //start with a nice default size
717 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
718 <a href="qlayoutitem.html">QLayoutItem</a> *o;
719 while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
720 ++it;
721<a name="x1528"></a><a name="x1515"></a> s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
722 }
723 return s + n*QSize(<a href="qlayout.html#spacing">spacing</a>(),spacing());
724}
725
726<a name="x1513"></a>QSize CardLayout::<a href="qlayout.html#minimumSize">minimumSize</a>() const
727{
728 <a href="qsize.html">QSize</a> s(0,0);
729 int n = list.<a href="qptrlist.html#count">count</a>();
730 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
731 <a href="qlayoutitem.html">QLayoutItem</a> *o;
732 while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
733 ++it;
734 s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
735 }
736 return s + n*QSize(<a href="qlayout.html#spacing">spacing</a>(),spacing());
737}
738</pre>
739
740<p> <hr>
741<p> Main:
742<p> <pre>/****************************************************************************
743** $Id: customlayout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
744**
745** Main for custom layout example
746**
747** Copyright (C) 1996-2007 Trolltech ASA. All rights reserved.
748**
749** This file is part of an example program for Qt. This example
750** program may be used, distributed and modified without limitation.
751**
752*****************************************************************************/
753
754#include "flow.h"
755#include "border.h"
756#include "card.h"
757
758#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
759#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
760#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
761#include &lt;<a href="qgroupbox-h.html">qgroupbox.h</a>&gt;
762#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
763#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
764#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
765
766int main( int argc, char **argv )
767{
768 <a href="qapplication.html">QApplication</a> a( argc, argv );
769
770 <a href="qwidget.html">QWidget</a> *f = new <a href="qwidget.html">QWidget</a>;
771 <a href="qboxlayout.html">QBoxLayout</a> *gm = new <a href="qvboxlayout.html">QVBoxLayout</a>( f, 5 );
772
773 SimpleFlow *b1 = new SimpleFlow( gm );
774
775<a name="x1536"></a> b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Short", f ) );
776 b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Longer", f ) );
777 b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Different text", f ) );
778 b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "More text", f ) );
779 b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Even longer button text", f ) );
780 <a href="qpushbutton.html">QPushButton</a>* qb = new <a href="qpushbutton.html">QPushButton</a>( "Quit", f );
781 a.<a href="qobject.html#connect">connect</a>( qb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), SLOT( quit() ) );
782 b1-&gt;<a href="qlayout.html#add">add</a>( qb );
783
784 <a href="qwidget.html">QWidget</a> *wid = new <a href="qwidget.html">QWidget</a>( f );
785
786 BorderLayout *large = new BorderLayout( wid );
787<a name="x1537"></a> large-&gt;<a href="qlayout.html#setSpacing">setSpacing</a>( 5 );
788 large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "North", wid ), BorderLayout::North );
789 large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "West", wid ), BorderLayout::West );
790 <a href="qmultilineedit.html">QMultiLineEdit</a>* m = new <a href="qmultilineedit.html">QMultiLineEdit</a>( wid );
791 m-&gt;<a href="qtextedit.html#setText">setText</a>( "Central\nWidget" );
792 large-&gt;addWidget( m, BorderLayout::Center );
793 <a href="qwidget.html">QWidget</a> *east1 = new <a href="qpushbutton.html">QPushButton</a>( "East", wid );
794 large-&gt;addWidget( east1, BorderLayout::East );
795 <a href="qwidget.html">QWidget</a> *east2 = new <a href="qpushbutton.html">QPushButton</a>( "East 2", wid );
796 large-&gt;addWidget( east2 , BorderLayout::East );
797 large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "South", wid ), BorderLayout::South );
798 //Left-to-right tab order looks better:
799<a name="x1542"></a> <a href="qwidget.html">QWidget</a>::<a href="qwidget.html#setTabOrder">setTabOrder</a>( east2, east1 );
800 gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( wid );
801
802
803 wid = new <a href="qwidget.html">QWidget</a>( f );
804 CardLayout *card = new CardLayout( wid, 10 );
805
806 <a href="qwidget.html">QWidget</a> *crd = new <a href="qwidget.html">QWidget</a>( wid );
807<a name="x1540"></a> crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::red );
808 card-&gt;<a href="qlayout.html#add">add</a>( crd );
809 crd = new <a href="qwidget.html">QWidget</a>( wid );
810 crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::green );
811 card-&gt;<a href="qlayout.html#add">add</a>( crd );
812 crd = new <a href="qwidget.html">QWidget</a>( wid );
813 crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::blue );
814 card-&gt;<a href="qlayout.html#add">add</a>( crd );
815 crd = new <a href="qwidget.html">QWidget</a>( wid );
816 crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::white );
817 card-&gt;<a href="qlayout.html#add">add</a>( crd );
818 crd = new <a href="qwidget.html">QWidget</a>( wid );
819 crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::black );
820 card-&gt;<a href="qlayout.html#add">add</a>( crd );
821 crd = new <a href="qwidget.html">QWidget</a>( wid );
822 crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::yellow );
823 card-&gt;<a href="qlayout.html#add">add</a>( crd );
824
825 gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( wid );
826
827 <a href="qlabel.html">QLabel</a>* s = new <a href="qlabel.html">QLabel</a>( f );
828 s-&gt;<a href="qlabel.html#setText">setText</a>( "outermost box" );
829 s-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
830 s-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( Qt::AlignVCenter | Qt::AlignHCenter );
831 gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( s );
832 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( f );
833 f-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Custom Layout");
834 f-&gt;<a href="qwidget.html#show">show</a>();
835
836 int result = a.<a href="qapplication.html#exec">exec</a>();
837 delete f;
838 return result;
839}
840</pre>
841
842<p>See also <a href="examples.html">Examples</a>.
843
844<!-- eof -->
845<p><address><hr><div align=center>
846<table width=100% cellspacing=0 border=0><tr>
847<td>Copyright &copy; 2007
848<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
849<td align=right><div align=right>Qt 3.3.8</div>
850</table></div></address></body>
851</html>
Note: See TracBrowser for help on using the repository browser.