Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 644

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/customlayout/customlayout.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customized Layoutmanager</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

 
<p> 
This examples demonstrates how to write customized layout (geometry) managers
like card layouts, border layout and flow layouts.
<p> See also: <a href="layout.html">Documentation of Geometry Management</a>.
<p> <hr>
<p> Header file of the flow layout:
<p> <pre>/****************************************************************************
** $Id:  qt/flow.h   3.0.2   edited Oct 12 12:18 $
**
** Definition of simple flow layout for custom layout example
**
** Created : 979899
**
** Copyright (C) 1997 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef FLOW_H
#define FLOW_H

#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;

class SimpleFlow : public <a href="qlayout.html">QLayout</a>
{
public:
    SimpleFlow( <a href="qwidget.html">QWidget</a> *parent, int border=0, int space=-1,
                const char *name=0 )
        : <a href="qlayout.html">QLayout</a>( parent, border, space, name ),
        cached_width(0) {}
    SimpleFlow( <a href="qlayout.html">QLayout</a>* parent, int space=-1, const char *name=0 )
        : <a href="qlayout.html">QLayout</a>( parent, space, name ),
        cached_width(0) {}
    SimpleFlow( int space=-1, const char *name=0 )
        : <a href="qlayout.html">QLayout</a>( space, name ),
        cached_width(0) {}

    ~SimpleFlow();

    void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item);
    bool hasHeightForWidth() const;
    int heightForWidth( int ) const;
    <a href="qsize.html">QSize</a> sizeHint() const;
    <a href="qsize.html">QSize</a> minimumSize() const;
    <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();
    QSizePolicy::ExpandData expanding() const;

protected:
    void setGeometry( const <a href="qrect.html">QRect</a>&amp; );

private:
    int doLayout( const <a href="qrect.html">QRect</a>&amp;, bool testonly = FALSE );
    <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; list;
    int cached_width;
    int cached_hfw;

};

#endif
</pre>

<p> <hr>
<p> Implementation of the flow layout:
<p> <pre>/****************************************************************************
** $Id:  qt/flow.cpp   3.0.2   edited Oct 12 12:18 $
**
** Implementing your own layout: flow example
**
** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "flow.h"

class SimpleFlowIterator :public <a href="qglayoutiterator.html">QGLayoutIterator</a>
{
public:
    SimpleFlowIterator( <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *l ) :idx(0), list(l)  {}
    uint count() const;
    <a href="qlayoutitem.html">QLayoutItem</a> *current();
    <a href="qlayoutitem.html">QLayoutItem</a> *next();
    <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();

private:
    int idx;
    <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *list;

};

uint <a name="f488"></a>SimpleFlowIterator::count() const
{
<a name="x1789"></a>    return list-&gt;<a href="qptrlist.html#count">count</a>();
}

<a name="x1774"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#current">current</a>()
{
<a name="x1788"></a>    return idx &lt; int(count()) ? list-&gt;<a href="qptrlist.html#at">at</a>(idx) : 0;
}

<a name="x1775"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#next">next</a>()
{
    idx++; return current();
}

<a name="x1776"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
{
<a name="x1790"></a>    return idx &lt; int(count()) ? list-&gt;<a href="qptrlist.html#take">take</a>( idx ) : 0;
}

SimpleFlow::~SimpleFlow()
{
    <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
}


<a name="x1783"></a>int SimpleFlow::<a href="qlayoutitem.html#heightForWidth">heightForWidth</a>( int w ) const
{
    if ( cached_width != w ) {
        //Not all C++ compilers support "mutable" yet:
        SimpleFlow * mthis = (SimpleFlow*)this;
        int h = mthis-&gt;doLayout( QRect(0,0,w,0), TRUE );
        mthis-&gt;cached_hfw = h;
        return h;
    }
    return cached_hfw;
}

<a name="x1777"></a>void SimpleFlow::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item)
{
<a name="x1787"></a>    list.<a href="qptrlist.html#append">append</a>( item );
}

<a name="x1782"></a>bool SimpleFlow::<a href="qlayoutitem.html#hasHeightForWidth">hasHeightForWidth</a>() const
{
    return TRUE;
}

<a name="x1786"></a>QSize SimpleFlow::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
{
    return minimumSize();
}

<a name="x1778"></a>QSizePolicy::ExpandData SimpleFlow::<a href="qlayout.html#expanding">expanding</a>() const
{
    return QSizePolicy::NoDirection;
}

<a name="x1779"></a>QLayoutIterator SimpleFlow::<a href="qlayout.html#iterator">iterator</a>()
{
    return QLayoutIterator( new SimpleFlowIterator( &amp;list ) );
}

<a name="x1781"></a>void SimpleFlow::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;r )
{
    QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( r );
    doLayout( r );
}

int <a name="f487"></a>SimpleFlow::doLayout( const <a href="qrect.html">QRect</a> &amp;r, bool testonly )
{
    int x = r.<a href="qrect.html#x">x</a>();
    int y = r.<a href="qrect.html#y">y</a>();
    int h = 0;          //height of this line so far.
    <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
    <a href="qlayoutitem.html">QLayoutItem</a> *o;
<a name="x1791"></a>    while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
        ++it;
        int nextX = x + o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width() + spacing();
<a name="x1792"></a>        if ( nextX - spacing() &gt; r.<a href="qrect.html#right">right</a>() &amp;&amp; h &gt; 0 ) {
            x = r.<a href="qrect.html#x">x</a>();
            y = y + h + spacing();
            nextX = x + o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width() + spacing();
            h = 0;
        }
        if ( !testonly )
<a name="x1785"></a>            o-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>( QRect( QPoint( x, y ), o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>() ) );
        x = nextX;
        h = QMAX( h,  o-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height() );
    }
    return y + h - r.<a href="qrect.html#y">y</a>();
}

<a name="x1780"></a>QSize SimpleFlow::<a href="qlayout.html#minimumSize">minimumSize</a>() const
{
    <a href="qsize.html">QSize</a> s(0,0);
    <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
    <a href="qlayoutitem.html">QLayoutItem</a> *o;
    while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
        ++it;
<a name="x1795"></a><a name="x1784"></a>        s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
    }
    return s;
}
</pre>

<p> <hr>
<p> Header file of the border layout:
<p> <pre>/****************************************************************************
** $Id:  qt/border.h   3.0.2   edited Oct 12 12:18 $
**
** Definition of simple flow layout for custom layout example
**
** Created : 979899
**
** Copyright (C) 1997 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef BORDER_H
#define BORDER_H

#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;

class BorderWidgetItem : public <a href="qwidgetitem.html">QWidgetItem</a>
{
public:
    BorderWidgetItem( <a href="qwidget.html">QWidget</a> *w )
        : <a href="qwidgetitem.html">QWidgetItem</a>( w )
    {}

    void setGeometry( const <a href="qrect.html">QRect</a> &amp;r )
    { widget()-&gt;setGeometry( r ); }

};

class BorderLayout : public <a href="qlayout.html">QLayout</a>
{
public:
    enum Position {
        West = 0,
        North,
        South,
        East,
        Center
    };

    struct BorderLayoutStruct
    {
        BorderLayoutStruct( <a href="qlayoutitem.html">QLayoutItem</a> *i, Position p ) {
            item = i;
            pos = p;
        }

        <a href="qlayoutitem.html">QLayoutItem</a> *item;
        Position pos;
    };

    enum SizeType {
        Minimum = 0,
        SizeHint
    };

    BorderLayout( <a href="qwidget.html">QWidget</a> *parent, int border = 0, int autoBorder = -1,
                  const char *name = 0 )
        : <a href="qlayout.html">QLayout</a>( parent, border, autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ),
          sizeDirty( TRUE ), msizeDirty( TRUE )
    {}

    BorderLayout( <a href="qlayout.html">QLayout</a>* parent, int autoBorder = -1, const char *name = 0 )
        : <a href="qlayout.html">QLayout</a>( parent, autoBorder, name  ), cached( 0, 0 ), mcached( 0, 0 ),
          sizeDirty( TRUE ), msizeDirty( TRUE )
    {}

    BorderLayout( int autoBorder = -1, const char *name = 0 )
        : <a href="qlayout.html">QLayout</a>( autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ),
          sizeDirty( TRUE ), msizeDirty( TRUE )
    {}

    ~BorderLayout();

    void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item );

    void addWidget( <a href="qwidget.html">QWidget</a> *widget, Position pos );
    void add( <a href="qlayoutitem.html">QLayoutItem</a> *item, Position pos );

    bool hasHeightForWidth() const;

    <a href="qsize.html">QSize</a> sizeHint() const;
    <a href="qsize.html">QSize</a> minimumSize() const;

    <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();

    QSizePolicy::ExpandData expanding() const;

protected:
    void setGeometry( const <a href="qrect.html">QRect</a> &amp;rect );

private:
    void doLayout( const <a href="qrect.html">QRect</a> &amp;rect, bool testonly = FALSE );
    void calcSize( SizeType st );

    <a href="qptrlist.html">QPtrList</a>&lt;BorderLayoutStruct&gt; list;
    <a href="qsize.html">QSize</a> cached, mcached;
    bool sizeDirty, msizeDirty;

};

#endif
</pre>

<p> <hr>
<p> Implementation of the border layout:
<p> <pre>/****************************************************************************
** $Id:  qt/border.cpp   3.0.2   edited Oct 12 12:18 $
**
** Implementing your own layout: flow example
**
** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "border.h"

class BorderLayoutIterator : public <a href="qglayoutiterator.html">QGLayoutIterator</a>
{
public:
    BorderLayoutIterator( const <a href="qptrlist.html">QPtrList</a>&lt;BorderLayout::BorderLayoutStruct&gt; *l )
        : idx( 0 ) , list( (QPtrList&lt;BorderLayout::BorderLayoutStruct&gt;*)l )
    {}

    uint count() const;
    <a href="qlayoutitem.html">QLayoutItem</a> *current();
    BorderLayout::BorderLayoutStruct *currentStruct();
    void toFirst();
    <a href="qlayoutitem.html">QLayoutItem</a> *next();
    <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();
    BorderLayoutIterator &amp;operator++();

private:
    int idx;
    <a href="qptrlist.html">QPtrList</a>&lt;BorderLayout::BorderLayoutStruct&gt; *list;

};

uint <a name="f492"></a>BorderLayoutIterator::count() const
{
<a name="x1812"></a>    return list-&gt;<a href="qptrlist.html#count">count</a>();
}

<a name="x1796"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#current">current</a>()
{
<a name="x1811"></a>    return idx &lt; (int)count() ? list-&gt;<a href="qptrlist.html#at">at</a>( idx )-&gt;item : 0;
}

BorderLayout::BorderLayoutStruct *<a name="f493"></a>BorderLayoutIterator::currentStruct()
{
    return idx &lt; (int)count() ? list-&gt;<a href="qptrlist.html#at">at</a>( idx ) : 0;
}

void <a name="f494"></a>BorderLayoutIterator::toFirst()
{
    idx = 0;
}

<a name="x1797"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#next">next</a>()
{
    idx++;
    return current();
}

<a name="x1798"></a>QLayoutItem *BorderLayoutIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
{
    BorderLayout::BorderLayoutStruct *b
<a name="x1813"></a>        = idx &lt; int( list-&gt;<a href="qptrlist.html#count">count</a>() ) ? list-&gt;<a href="qptrlist.html#take">take</a>(  idx  ) : 0;
    <a href="qlayoutitem.html">QLayoutItem</a> *item =  b ? b-&gt;item : 0;
    delete b;
    return item;
}

BorderLayoutIterator &amp;BorderLayoutIterator::operator++()
{
    next();
    return *this;
}

BorderLayout::~BorderLayout()
{
    <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
}


<a name="x1800"></a>void BorderLayout::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item )
{
    <a href="qlayout.html#add">add</a>( item, West );
}

void <a name="f489"></a>BorderLayout::addWidget( <a href="qwidget.html">QWidget</a> *widget, Position pos )
{
    <a href="qlayout.html#add">add</a>( new BorderWidgetItem( widget ), pos );
}

<a name="x1799"></a>void BorderLayout::<a href="qlayout.html#add">add</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item, Position pos )
{
<a name="x1810"></a>    list.<a href="qptrlist.html#append">append</a>( new BorderLayoutStruct( item, pos ) );
    sizeDirty = TRUE; msizeDirty = TRUE;
    calcSize( SizeHint ); calcSize( Minimum );
}

<a name="x1806"></a>bool BorderLayout::<a href="qlayoutitem.html#hasHeightForWidth">hasHeightForWidth</a>() const
{
    return FALSE;
}

<a name="x1809"></a>QSize BorderLayout::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
{
    return cached;
}

<a name="x1803"></a>QSize BorderLayout::<a href="qlayout.html#minimumSize">minimumSize</a>() const
{
    return cached;
}

<a name="x1801"></a>QSizePolicy::ExpandData BorderLayout::<a href="qlayout.html#expanding">expanding</a>() const

{
    return QSizePolicy::BothDirections;
}

<a name="x1802"></a>QLayoutIterator BorderLayout::<a href="qlayout.html#iterator">iterator</a>()
{
    return QLayoutIterator( new BorderLayoutIterator( &amp;list ) );
}

<a name="x1804"></a>void BorderLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;rct )
{
    QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( rct );
    doLayout( rct );
}

void <a name="f490"></a>BorderLayout::doLayout( const <a href="qrect.html">QRect</a> &amp;rct, bool /*testonly*/ )
{
    int ew = 0, ww = 0, nh = 0, sh = 0;
    int h = 0;

    BorderLayoutIterator it( &amp;list );
    BorderLayoutStruct *o;
    BorderLayoutStruct *center = 0;
    while ( ( o = it.currentStruct() ) != 0 ) {
        ++it;

        if ( o-&gt;pos == North ) {
<a name="x1816"></a><a name="x1815"></a><a name="x1808"></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() ) );
<a name="x1805"></a>            nh += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() + spacing();
        }
        if ( o-&gt;pos == South ) {
            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(),
                                         rct.<a href="qrect.html#width">width</a>(), o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height() ) );
            sh += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() + spacing();
            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(),
                                         o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() ) );
        }
        if ( o-&gt;pos == Center )
            center = o;
    }

    h = rct.<a href="qrect.html#height">height</a>() - nh - sh;

    it.toFirst();
    while ( ( o = it.currentStruct() ) != 0 ) {
        ++it;

        if ( o-&gt;pos == West ) {
            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 ) );
            ww += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width() + spacing();
        }
        if ( o-&gt;pos == East ) {
            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(),
                                         o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width(), h ) );
            ew += o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width() + spacing();
            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,
                                         o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().width(), o-&gt;item-&gt;<a href="qlayoutitem.html#geometry">geometry</a>().height() ) );
        }
    }

    if ( center )
        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 ) );
}

void <a name="f491"></a>BorderLayout::calcSize( SizeType st )
{
    if ( ( st == Minimum &amp;&amp; !msizeDirty ) ||
         ( st == SizeHint &amp;&amp; !sizeDirty ) )
        return;

    int w = 0, h = 0;

    BorderLayoutIterator it( &amp;list );
    BorderLayoutStruct *o;
    while ( ( o = it.currentStruct() ) != 0 ) {
        ++it;
        if ( o-&gt;pos == North ||
             o-&gt;pos == South ) {
            if ( st == Minimum )
<a name="x1807"></a>                h += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().height();
            else
                h += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height();
        }
        else if ( o-&gt;pos == West ||
                  o-&gt;pos == East ) {
            if ( st == Minimum )
                w += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().width();
            else
                w += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width();
        } else {
            if ( st == Minimum ) {
                h += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().height();
                w += o-&gt;item-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>().width();
            }
            else {
                h += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().height();
                w += o-&gt;item-&gt;<a href="qlayoutitem.html#sizeHint">sizeHint</a>().width();
            }
        }
    }

    if ( st == Minimum ) {
        msizeDirty = FALSE;
        mcached = QSize( w, h );
    } else {
        sizeDirty = FALSE;
        cached = QSize( w, h );
    }

    return;
}
</pre>

<p> <hr>
<p> Header file of the card layout:
<p> <pre>/****************************************************************************
** $Id:  qt/card.h   3.0.2   edited Oct 12 12:18 $
**
** Definition of simple flow layout for custom layout example
**
** Created : 979899
**
** Copyright (C) 1997 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef CARD_H
#define CARD_H

#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;

class CardLayout : public <a href="qlayout.html">QLayout</a>
{
public:
    CardLayout( <a href="qwidget.html">QWidget</a> *parent, int dist )
        : <a href="qlayout.html">QLayout</a>( parent, 0, dist ) {}
    CardLayout( <a href="qlayout.html">QLayout</a>* parent, int dist)
        : <a href="qlayout.html">QLayout</a>( parent, dist ) {}
    CardLayout( int dist )
        : <a href="qlayout.html">QLayout</a>( dist ) {}
    ~CardLayout();

    void addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item );
    <a href="qsize.html">QSize</a> sizeHint() const;
    <a href="qsize.html">QSize</a> minimumSize() const;
    <a href="qlayoutiterator.html">QLayoutIterator</a> iterator();
    void setGeometry( const <a href="qrect.html">QRect</a> &amp;rect );

private:
    <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; list;

};

#endif
</pre>

<p> <hr>
<p> Implementation of the card layout:
<p> <pre>/****************************************************************************
** $Id:  qt/card.cpp   3.0.2   edited Oct 12 12:18 $
**
** Implementing your own layout: flow example
**
** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "card.h"

class CardLayoutIterator :public <a href="qglayoutiterator.html">QGLayoutIterator</a>
{
public:
    CardLayoutIterator( <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *l )
        : idx( 0 ), list( l )  {}

    <a href="qlayoutitem.html">QLayoutItem</a> *current();
    <a href="qlayoutitem.html">QLayoutItem</a> *next();
    <a href="qlayoutitem.html">QLayoutItem</a> *takeCurrent();

private:
    int idx;
    <a href="qptrlist.html">QPtrList</a>&lt;QLayoutItem&gt; *list;
};

<a name="x1818"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#current">current</a>()
{
<a name="x1830"></a><a name="x1829"></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;
}

<a name="x1819"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#next">next</a>()
{
    idx++; return current();
}

<a name="x1820"></a>QLayoutItem *CardLayoutIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>()
{
<a name="x1831"></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;
}



<a name="x1822"></a>QLayoutIterator CardLayout::<a href="qlayout.html#iterator">iterator</a>()
{
    return QLayoutIterator(  new CardLayoutIterator( &amp;list )  );
}

CardLayout::~CardLayout()
{
    <a href="qlayout.html#deleteAllItems">deleteAllItems</a>();
}

<a name="x1821"></a>void CardLayout::<a href="qlayout.html#addItem">addItem</a>(  <a href="qlayoutitem.html">QLayoutItem</a> *item  )
{
<a name="x1828"></a>    list.<a href="qptrlist.html#append">append</a>( item );
}

<a name="x1824"></a>void CardLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( const <a href="qrect.html">QRect</a> &amp;rct )
{
    QLayout::<a href="qlayout.html#setGeometry">setGeometry</a>( rct );

    <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it( list );
<a name="x1832"></a>    if ( it.<a href="qptrlistiterator.html#count">count</a>() == 0 )
        return;

    <a href="qlayoutitem.html">QLayoutItem</a> *o;

    int i = 0;

    int w = rct.<a href="qrect.html#width">width</a>() - ( list.<a href="qptrlist.html#count">count</a>() - 1 ) * spacing();
    int h = rct.<a href="qrect.html#height">height</a>() - ( list.<a href="qptrlist.html#count">count</a>() - 1 ) * spacing();

<a name="x1833"></a>    while ( ( o=it.<a href="qptrlistiterator.html#current">current</a>() ) != 0 ) {
        ++it;
        <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(),
                    w, h  );
<a name="x1826"></a>        o-&gt;<a href="qlayoutitem.html#setGeometry">setGeometry</a>(  geom  );
        ++i;
    }
}

<a name="x1827"></a>QSize CardLayout::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const
{
    <a href="qsize.html">QSize</a> s(0,0);
    int n = list.<a href="qptrlist.html#count">count</a>();
    if ( n &gt; 0 )
        s = QSize(100,70); //start with a nice default size
    <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
    <a href="qlayoutitem.html">QLayoutItem</a> *o;
    while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
        ++it;
<a name="x1838"></a><a name="x1825"></a>        s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
    }
    return s + n*QSize(<a href="qlayout.html#spacing">spacing</a>(),spacing());
}

<a name="x1823"></a>QSize CardLayout::<a href="qlayout.html#minimumSize">minimumSize</a>() const
{
    <a href="qsize.html">QSize</a> s(0,0);
    int n = list.<a href="qptrlist.html#count">count</a>();
    <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QLayoutItem&gt; it(list);
    <a href="qlayoutitem.html">QLayoutItem</a> *o;
    while ( (o=it.<a href="qptrlistiterator.html#current">current</a>()) != 0 ) {
        ++it;
        s = s.<a href="qsize.html#expandedTo">expandedTo</a>( o-&gt;<a href="qlayoutitem.html#minimumSize">minimumSize</a>() );
    }
    return s + n*QSize(<a href="qlayout.html#spacing">spacing</a>(),spacing());
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/main.cpp   3.0.2   edited Oct 12 12:18 $
**
** Main for custom layout example
**
** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "flow.h"
#include "border.h"
#include "card.h"

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
#include &lt;<a href="qgroupbox-h.html">qgroupbox.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;

int main( int argc, char **argv )
{
    <a href="qapplication.html">QApplication</a> a( argc, argv );

    <a href="qwidget.html">QWidget</a> *f = new <a href="qwidget.html">QWidget</a>;
    <a href="qboxlayout.html">QBoxLayout</a> *gm = new <a href="qvboxlayout.html">QVBoxLayout</a>( f, 5 );

    SimpleFlow *b1 = new SimpleFlow( gm );

<a name="x1846"></a>    b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Short", f ) );
    b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Longer", f ) );
    b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Different text", f ) );
    b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "More text", f ) );
    b1-&gt;<a href="qlayout.html#add">add</a>( new <a href="qpushbutton.html">QPushButton</a>( "Even longer button text", f ) );
    <a href="qpushbutton.html">QPushButton</a>* qb = new <a href="qpushbutton.html">QPushButton</a>( "Quit", f );
    a.<a href="qobject.html#connect">connect</a>( qb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), SLOT( quit() ) );
    b1-&gt;<a href="qlayout.html#add">add</a>( qb );

    <a href="qwidget.html">QWidget</a> *wid = new <a href="qwidget.html">QWidget</a>( f );

    BorderLayout *large = new BorderLayout( wid );
<a name="x1847"></a>    large-&gt;<a href="qlayout.html#setSpacing">setSpacing</a>( 5 );
    large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "North", wid ), BorderLayout::North );
    large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "West", wid ), BorderLayout::West );
    <a href="qmultilineedit.html">QMultiLineEdit</a>* m = new <a href="qmultilineedit.html">QMultiLineEdit</a>( wid );
    m-&gt;<a href="qtextedit.html#setText">setText</a>( "Central\nWidget" );
    large-&gt;addWidget( m, BorderLayout::Center );
    <a href="qwidget.html">QWidget</a> *east1 = new <a href="qpushbutton.html">QPushButton</a>( "East", wid );
    large-&gt;addWidget( east1, BorderLayout::East );
    <a href="qwidget.html">QWidget</a> *east2 = new <a href="qpushbutton.html">QPushButton</a>( "East 2", wid );
    large-&gt;addWidget( east2 , BorderLayout::East );
    large-&gt;addWidget( new <a href="qpushbutton.html">QPushButton</a>( "South", wid ), BorderLayout::South );
    //Left-to-right tab order looks better:
<a name="x1852"></a>    <a href="qwidget.html">QWidget</a>::<a href="qwidget.html#setTabOrder">setTabOrder</a>( east2, east1 );
    gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( wid );


    wid = new <a href="qwidget.html">QWidget</a>( f );
    CardLayout *card = new CardLayout( wid, 10 );

    <a href="qwidget.html">QWidget</a> *crd = new <a href="qwidget.html">QWidget</a>( wid );
<a name="x1850"></a>    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::red );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );
    crd = new <a href="qwidget.html">QWidget</a>( wid );
    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::green );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );
    crd = new <a href="qwidget.html">QWidget</a>( wid );
    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::blue );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );
    crd = new <a href="qwidget.html">QWidget</a>( wid );
    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::white );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );
    crd = new <a href="qwidget.html">QWidget</a>( wid );
    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::black );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );
    crd = new <a href="qwidget.html">QWidget</a>( wid );
    crd-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::yellow );
    card-&gt;<a href="qlayout.html#add">add</a>( crd );

    gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( wid );

    <a href="qlabel.html">QLabel</a>* s = new <a href="qlabel.html">QLabel</a>( f );
    s-&gt;<a href="qlabel.html#setText">setText</a>( "outermost box" );
    s-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
<a name="x1844"></a>    s-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( Qt::AlignVCenter | Qt::AlignHCenter );
    gm-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( s );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( f );
    f-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Custom Layout");
    f-&gt;<a href="qwidget.html#show">show</a>();

    int result = a.<a href="qapplication.html#exec">exec</a>();
    delete f;
    return result;
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>