customlayout-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 851 行 · 第 1/3 页

HTML
851
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/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) managerslike 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.5   edited Oct 12 2001 $**** 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.5   edited Oct 12 2001 $**** 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="f424"></a>SimpleFlowIterator::count() const{<a name="x1350"></a>    return list-&gt;<a href="qptrlist.html#count">count</a>();}<a name="x1335"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#current">current</a>(){<a name="x1349"></a>    return idx &lt; int(count()) ? list-&gt;<a href="qptrlist.html#at">at</a>(idx) : 0;}<a name="x1336"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#next">next</a>(){    idx++; return current();}<a name="x1337"></a>QLayoutItem *SimpleFlowIterator::<a href="qglayoutiterator.html#takeCurrent">takeCurrent</a>(){<a name="x1351"></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="x1344"></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="x1338"></a>void SimpleFlow::<a href="qlayout.html#addItem">addItem</a>( <a href="qlayoutitem.html">QLayoutItem</a> *item){<a name="x1348"></a>    list.<a href="qptrlist.html#append">append</a>( item );}<a name="x1343"></a>bool SimpleFlow::<a href="qlayoutitem.html#hasHeightForWidth">hasHeightForWidth</a>() const{    return TRUE;}<a name="x1347"></a>QSize SimpleFlow::<a href="qlayoutitem.html#sizeHint">sizeHint</a>() const{    return minimumSize();}<a name="x1339"></a>QSizePolicy::ExpandData SimpleFlow::<a href="qlayout.html#expanding">expanding</a>() const{    return QSizePolicy::NoDirection;}<a name="x1340"></a>QLayoutIterator SimpleFlow::<a href="qlayout.html#iterator">iterator</a>(){    return QLayoutIterator( new SimpleFlowIterator( &amp;list ) );}<a name="x1342"></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="f423"></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="x1352"></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="x1353"></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="x1346"></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="x1341"></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="x1356"></a><a name="x1345"></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.5   edited Oct 12 2001 $**** 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;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?