📄 customlayout-main-cpp.html
字号:
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(); <a href="qsizepolicy.html#ExpandData">QSizePolicy::ExpandData</a> expanding() const;protected: void setGeometry( const QRect &rect );private: void doLayout( const QRect &rect, bool testonly = FALSE ); void calcSize( SizeType st ); <a href="qlist.html">QList</a><BorderLayoutStruct> list; <a href="qsize.html">QSize</a> cached, mcached; bool sizeDirty, msizeDirty;};#endif</pre> <hr> Implementation of the Border-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/border.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 "border.h"class BorderLayoutIterator : public QGLayoutIterator{public: BorderLayoutIterator( const QList<BorderLayout::BorderLayoutStruct> *l ) : idx( 0 ) , list( (<a href="qlist.html">QList</a><BorderLayout::BorderLayoutStruct>*)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 &operator++();private: int idx; <a href="qlist.html">QList</a><BorderLayout::BorderLayoutStruct> *list;};uint <a name="273"></a>BorderLayoutIterator::count() const{ return list-><a href="qlist.html#359d9f">count</a>();}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="274"></a>BorderLayoutIterator::current(){ return idx < (int)count() ? list-><a href="qlist.html#0e7e42">at</a>( idx )->item : 0;}BorderLayout::BorderLayoutStruct *<a name="275"></a>BorderLayoutIterator::currentStruct(){ return idx < (int)count() ? list-><a href="qlist.html#0e7e42">at</a>( idx ) : 0;}void <a name="276"></a>BorderLayoutIterator::toFirst(){ idx = 0;}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="277"></a>BorderLayoutIterator::next(){ idx++; return current();}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="278"></a>BorderLayoutIterator::takeCurrent(){ BorderLayout::BorderLayoutStruct *b = idx < int( list-><a href="qlist.html#359d9f">count</a>() ) ? list-><a href="qlist.html#0639ad">take</a>( idx ) : 0; <a href="qlayoutitem.html">QLayoutItem</a> *item = b ? b->item : 0; delete b; return item;}BorderLayoutIterator &<a href=#279><a name="279"></a>BorderLayoutIterator::operator</a>++(){ <a href=#277>next</a>(); return *this;}BorderLayout::~BorderLayout(){ deleteAllItems();}void <a name="280"></a>BorderLayout::addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item ){ <a href=#282>add</a>( item, West );}void <a name="281"></a>BorderLayout::addWidget( <a href="qwidget.html">QWidget</a> *widget, Position pos ){ <a href=#282>add</a>( new BorderWidgetItem( widget ), pos );}void <a name="282"></a>BorderLayout::add( <a href="qlayoutitem.html">QLayoutItem</a> *item, Position pos ){ list.<a href="qlist.html#c5746a">append</a>( new BorderLayoutStruct( item, pos ) ); sizeDirty = TRUE; msizeDirty = TRUE; <a href=#290>calcSize</a>( SizeHint ); calcSize( Minimum );}bool <a name="283"></a>BorderLayout::hasHeightForWidth() const{ return FALSE;}<a href="qsize.html">QSize</a> <a name="284"></a>BorderLayout::sizeHint() const{ return cached;}<a href="qsize.html">QSize</a> <a name="285"></a>BorderLayout::minimumSize() const{ return cached;}<a href="qsizepolicy.html#ExpandData">QSizePolicy::ExpandData</a> <a name="286"></a>BorderLayout::expanding() const{ return QSizePolicy::BothDirections;}<a href="qlayoutiterator.html">QLayoutIterator</a> <a name="287"></a>BorderLayout::iterator(){ return QLayoutIterator( new BorderLayoutIterator( &list ) );}void <a name="288"></a>BorderLayout::setGeometry( const QRect &rct ){ <a href="qlayout.html#f1f752">QLayout::setGeometry</a>( rct ); <a href=#289>doLayout</a>( rct );}void <a name="289"></a>BorderLayout::doLayout( const QRect &rct, bool /*testonly*/ ){ int ew = 0, ww = 0, nh = 0, sh = 0; int h = 0; BorderLayoutIterator it = BorderLayoutIterator( &list ); BorderLayoutStruct *o; BorderLayoutStruct *center = 0; while ( ( o = it.currentStruct() ) != 0 ) { ++it; if ( o->pos == North ) { o->item->setGeometry( <a href="qrect.html">QRect</a>( rct.x(), nh, rct.width(), o->item->sizeHint().height() ) ); nh += o->item->geometry().height() + spacing(); } if ( o->pos == South ) { o->item->setGeometry( <a href="qrect.html">QRect</a>( o->item->geometry().x(), o->item->geometry().y(), rct.width(), o->item->sizeHint().height() ) ); sh += o->item->geometry().height() + spacing(); o->item->setGeometry( <a href="qrect.html">QRect</a>( rct.x(), rct.y() + rct.height() - sh + spacing(), o->item->geometry().width(), o->item->geometry().height() ) ); } if ( o->pos == Center ) center = o; } h = rct.height() - nh - sh; it.toFirst(); while ( ( o = it.currentStruct() ) != 0 ) { ++it; if ( o->pos == West ) { o->item->setGeometry( <a href="qrect.html">QRect</a>( rct.x() + ww, nh, o->item->sizeHint().width(), h ) ); ww += o->item->geometry().width() + spacing(); } if ( o->pos == East ) { o->item->setGeometry( <a href="qrect.html">QRect</a>( o->item->geometry().x(), o->item->geometry().y(), o->item->sizeHint().width(), h ) ); ew += o->item->geometry().width() + spacing(); o->item->setGeometry( <a href="qrect.html">QRect</a>( rct.x() + rct.width() - ew + spacing(), nh, o->item->geometry().width(), o->item->geometry().height() ) ); } } if ( center ) center->item->setGeometry( <a href="qrect.html">QRect</a>( ww, nh, rct.width() - ew - ww, h ) );}void <a name="290"></a>BorderLayout::calcSize( SizeType st ){ if ( ( st == Minimum && !msizeDirty ) || ( st == SizeHint && !sizeDirty ) ) return; int w = 0, h = 0; BorderLayoutIterator it = BorderLayoutIterator( &list ); BorderLayoutStruct *o; while ( ( o = it.currentStruct() ) != 0 ) { ++it; if ( o->pos == North || o->pos == South ) { if ( st == Minimum ) h += o->item->minimumSize().height(); else h += o->item->sizeHint().height(); } else if ( o->pos == West || o->pos == East ) { if ( st == Minimum ) w += o->item->minimumSize().width(); else w += o->item->sizeHint().width(); } else { if ( st == Minimum ) { h += o->item->minimumSize().height(); w += o->item->minimumSize().width(); } else { h += o->item->sizeHint().height(); w += o->item->sizeHint().width(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -