📄 customlayout-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - customlayout/main.cpp example file</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: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Customized Layoutmanager</h1><br clear="all"> This examples demonstrates how to write a customized layout (geometry) manager, like a Card-Layout, Border-Layout and Flow-Layout. See also: <a href="layout.html">Documentation of Geometry Management</a>. <hr> Header file of the Flow-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/flow.h 2.3.8 edited 2004-05-12 $**** 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 <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qlist-h.html">qlist.h</a>>class SimpleFlow : public QLayout{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(); <a href="qsizepolicy.html#ExpandData">QSizePolicy::ExpandData</a> expanding() const;protected: void setGeometry( const QRect& );private: int doLayout( const QRect&, bool testonly = FALSE ); <a href="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> list; int cached_width; int cached_hfw;};#endif</pre> <hr> Implementation of the Flow-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/flow.cpp 2.3.8 edited 2004-05-12 $**** 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 QGLayoutIterator{public: SimpleFlowIterator( <a href="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> *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="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> *list;};uint <a name="269"></a>SimpleFlowIterator::count() const{ return list-><a href="qlist.html#359d9f">count</a>();}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="270"></a>SimpleFlowIterator::current(){ return idx < int(<a href=#269>count</a>()) ? list-><a href="qlist.html#0e7e42">at</a>(idx) : 0;}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="271"></a>SimpleFlowIterator::next(){ idx++; return current();}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="272"></a>SimpleFlowIterator::takeCurrent(){ return idx < int(<a href=#269>count</a>()) ? list-><a href="qlist.html#0639ad">take</a>( idx ) : 0;}SimpleFlow::~SimpleFlow(){ deleteAllItems();}int <a name="260"></a>SimpleFlow::heightForWidth( int w ) const{ if ( cached_width != w ) { //Not all C++ compilers support "mutable" yet: SimpleFlow * mthis = (SimpleFlow*)this; int h = mthis->doLayout( <a href="qrect.html">QRect</a>(0,0,w,0), TRUE ); mthis->cached_hfw = h; return h; } return cached_hfw;}void <a name="261"></a>SimpleFlow::addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item){ list.<a href="qlist.html#c5746a">append</a>( item );}bool <a name="262"></a>SimpleFlow::hasHeightForWidth() const{ return TRUE;}<a href="qsize.html">QSize</a> <a name="263"></a>SimpleFlow::sizeHint() const{ return minimumSize();}<a href="qsizepolicy.html#ExpandData">QSizePolicy::ExpandData</a> <a name="264"></a>SimpleFlow::expanding() const{ return QSizePolicy::NoDirection;}<a href="qlayoutiterator.html">QLayoutIterator</a> <a name="265"></a>SimpleFlow::iterator(){ return QLayoutIterator( new SimpleFlowIterator( &list ) );}void <a name="266"></a>SimpleFlow::setGeometry( const QRect &r ){ <a href="qlayout.html#f1f752">QLayout::setGeometry</a>( r ); <a href=#267>doLayout</a>( r );}int <a name="267"></a>SimpleFlow::doLayout( const QRect &r, bool testonly ){ int x = r.x(); int y = r.y(); int h = 0; //height of this line so far. <a href="qlistiterator.html">QListIterator</a><<a href="qlayoutitem.html">QLayoutItem</a>> it(list); <a href="qlayoutitem.html">QLayoutItem</a> *o; while ( (o=it.<a href="qlistiterator.html#e58f06">current</a>()) != 0 ) { ++it; int nextX = x + o-><a href="qlayoutitem.html#48b5b2">sizeHint</a>().width() + spacing(); if ( nextX - spacing() > r.right() && h > 0 ) { x = r.x(); y = y + h + spacing(); nextX = x + o-><a href="qlayoutitem.html#48b5b2">sizeHint</a>().width() + spacing(); h = 0; } if ( !testonly ) o-><a href="qlayoutitem.html#0c6928">setGeometry</a>( <a href="qrect.html">QRect</a>( <a href="qpoint.html">QPoint</a>( x, y ), o-><a href="qlayoutitem.html#48b5b2">sizeHint</a>() ) ); x = nextX; h = QMAX( h, o-><a href="qlayoutitem.html#48b5b2">sizeHint</a>().height() ); } return y + h - r.y();}<a href="qsize.html">QSize</a> <a name="268"></a>SimpleFlow::minimumSize() const{ <a href="qsize.html">QSize</a> s(0,0); <a href="qlistiterator.html">QListIterator</a><<a href="qlayoutitem.html">QLayoutItem</a>> it(list); <a href="qlayoutitem.html">QLayoutItem</a> *o; while ( (o=it.<a href="qlistiterator.html#e58f06">current</a>()) != 0 ) { ++it; s = s.<a href="qsize.html#a74829">expandedTo</a>( o-><a href="qlayoutitem.html#83838c">minimumSize</a>() ); } return s;}</pre> <hr> Header file of the Border-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/border.h 2.3.8 edited 2004-05-12 $**** 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 <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qlist-h.html">qlist.h</a>>class BorderWidgetItem : public QWidgetItem{public: BorderWidgetItem( <a href="qwidget.html">QWidget</a> *w ) : <a href="qwidgetitem.html">QWidgetItem</a>( w ) {} void setGeometry( const QRect &r ) { widget()->setGeometry( r ); }};class BorderLayout : public QLayout{public: enum Position { West = 0, North, South, East, Center }; struct BorderLayoutStruct { BorderLayoutStruct( <a href="qlayoutitem.html">QLayoutItem</a> *i, Position p ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -