📄 customlayout-main-cpp.html
字号:
if ( st == Minimum ) { msizeDirty = FALSE; mcached = QSize( w, h ); } else { sizeDirty = FALSE; cached = QSize( w, h ); } return;}</pre> <hr> Header file of the Card-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/card.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 CARD_H#define CARD_H#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qlist-h.html">qlist.h</a>>class CardLayout : public QLayout{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 QRect &rect );private: <a href="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> list;};#endif</pre> <hr> Implementation of the Card-Layout: <pre>/****************************************************************************** $Id: qt/examples/customlayout/card.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 "card.h"class CardLayoutIterator :public QGLayoutIterator{public: CardLayoutIterator( <a href="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> *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="qlist.html">QList</a><<a href="qlayoutitem.html">QLayoutItem</a>> *list;};<a href="qlayoutitem.html">QLayoutItem</a> *<a name="291"></a>CardLayoutIterator::current(){ return idx < int( list-><a href="qlist.html#359d9f">count</a>() ) ? list-><a href="qlist.html#0e7e42">at</a>( idx ) : 0;}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="292"></a>CardLayoutIterator::next(){ idx++; return current();}<a href="qlayoutitem.html">QLayoutItem</a> *<a name="293"></a>CardLayoutIterator::takeCurrent(){ return idx < int( list-><a href="qlist.html#359d9f">count</a>() ) ?list-><a href="qlist.html#0639ad">take</a>( idx ) : 0;}<a href="qlayoutiterator.html">QLayoutIterator</a> <a name="294"></a>CardLayout::iterator(){ return QLayoutIterator( new CardLayoutIterator( &list ) );}CardLayout::~CardLayout(){ deleteAllItems();}void <a name="295"></a>CardLayout::addItem( <a href="qlayoutitem.html">QLayoutItem</a> *item ){ list.<a href="qlist.html#c5746a">append</a>( item );}void <a name="296"></a>CardLayout::setGeometry( const QRect &rct ){ <a href="qlayout.html#f1f752">QLayout::setGeometry</a>( rct ); <a href="qlistiterator.html">QListIterator</a><<a href="qlayoutitem.html">QLayoutItem</a>> it( list ); if ( it.<a href="qlistiterator.html#e61e8b">count</a>() == 0 ) return; <a href="qlayoutitem.html">QLayoutItem</a> *o; int i = 0; int w = rct.width() - ( list.<a href="qlist.html#359d9f">count</a>() - 1 ) * spacing(); int h = rct.height() - ( list.<a href="qlist.html#359d9f">count</a>() - 1 ) * spacing(); while ( ( o=it.<a href="qlistiterator.html#e58f06">current</a>() ) != 0 ) { ++it; <a href="qrect.html">QRect</a> geom( rct.x() + i * spacing(), rct.y() + i * spacing(), w, h ); o-><a href="qlayoutitem.html#0c6928">setGeometry</a>( geom ); ++i; }}<a href="qsize.html">QSize</a> <a name="297"></a>CardLayout::sizeHint() const{ <a href="qsize.html">QSize</a> s(0,0); int n = list.<a href="qlist.html#359d9f">count</a>(); if ( n > 0 ) s = QSize(100,70); //start with a nice default size <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 + n*QSize(<a href="qlayout.html#372777">spacing</a>(),<a href="qlayout.html#372777">spacing</a>());}<a href="qsize.html">QSize</a> <a name="298"></a>CardLayout::minimumSize() const{ <a href="qsize.html">QSize</a> s(0,0); int n = list.<a href="qlist.html#359d9f">count</a>(); <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 + n*QSize(<a href="qlayout.html#372777">spacing</a>(),<a href="qlayout.html#372777">spacing</a>());}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/customlayout/main.cpp 2.3.8 edited 2004-05-12 $**** 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>>#include <<a name="qcolor.h"></a><a href="qcolor-h.html">qcolor.h</a>>#include <<a name="qgroupbox.h"></a><a href="qgroupbox-h.html">qgroupbox.h</a>>#include <<a name="qpushbutton.h"></a><a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a name="qmultilineedit.h"></a><a href="qmultilineedit-h.html">qmultilineedit.h</a>>#include <<a href="qcolor-h.html">qcolor.h</a>>int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); <a name="QWidget"></a><a href="qwidget.html">QWidget</a> *f = new <a href="qwidget.html">QWidget</a>; <a name="QBoxLayout"></a><a href="qboxlayout.html">QBoxLayout</a> *gm = new <a name="QVBoxLayout"></a><a href="qvboxlayout.html">QVBoxLayout</a>( f, 5 ); SimpleFlow *b1 = new SimpleFlow( gm ); b1-><a name="add"></a><a href="qlayout.html#a9ddc3">add</a>( new QPushButton( "Short", f ) ); b1-><a href="qlayout.html#a9ddc3">add</a>( new QPushButton( "Longer", f ) ); b1-><a href="qlayout.html#a9ddc3">add</a>( new QPushButton( "Different text", f ) ); b1-><a href="qlayout.html#a9ddc3">add</a>( new QPushButton( "More text", f ) ); b1-><a href="qlayout.html#a9ddc3">add</a>( new QPushButton( "Even longer button text", f ) ); <a name="QPushButton"></a><a href="qpushbutton.html">QPushButton</a>* qb = new <a href="qpushbutton.html">QPushButton</a>( "Quit", f ); a.<a name="connect"></a><a href="qobject.html#fbde73">connect</a>( qb, SIGNAL( clicked() ), SLOT( quit() ) ); b1-><a href="qlayout.html#a9ddc3">add</a>( qb ); <a href="qwidget.html">QWidget</a> *wid = new <a href="qwidget.html">QWidget</a>( f ); BorderLayout *large = new BorderLayout( wid ); large->setSpacing( 5 ); large->addWidget( new QPushButton( "North", wid ), BorderLayout::North ); large->addWidget( new QPushButton( "West", wid ), BorderLayout::West ); <a name="QMultiLineEdit"></a><a href="qmultilineedit.html">QMultiLineEdit</a>* m = new <a href="qmultilineedit.html">QMultiLineEdit</a>( wid ); m-><a name="setText"></a><a href="qmultilineedit.html#3fd8c1">setText</a>( "Central\nWidget" ); large->addWidget( m, BorderLayout::Center ); <a href="qwidget.html">QWidget</a> *east1 = new <a href="qpushbutton.html">QPushButton</a>( "East", wid ); large->addWidget( east1, BorderLayout::East ); <a href="qwidget.html">QWidget</a> *east2 = new <a href="qpushbutton.html">QPushButton</a>( "East 2", wid ); large->addWidget( east2 , BorderLayout::East ); large->addWidget( new QPushButton( "South", wid ), BorderLayout::South ); //Left-to-right tab order looks better: <a name="QWidget::setTabOrder"></a><a href="qwidget.html#20c984">QWidget::setTabOrder</a>( east2, east1 ); gm-><a name="addWidget"></a><a href="qboxlayout.html#ebba99">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 ); crd-><a name="setBackgroundColor"></a><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::red ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); crd = new <a href="qwidget.html">QWidget</a>( wid ); crd-><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::green ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); crd = new <a href="qwidget.html">QWidget</a>( wid ); crd-><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::blue ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); crd = new <a href="qwidget.html">QWidget</a>( wid ); crd-><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::white ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); crd = new <a href="qwidget.html">QWidget</a>( wid ); crd-><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::black ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); crd = new <a href="qwidget.html">QWidget</a>( wid ); crd-><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::yellow ); card-><a href="qlayout.html#a9ddc3">add</a>( crd ); gm-><a href="qboxlayout.html#ebba99">addWidget</a>( wid ); <a name="QLabel"></a><a href="qlabel.html">QLabel</a>* s = new <a href="qlabel.html">QLabel</a>( f ); s-><a name="setText"></a><a href="qlabel.html#bc5ea6">setText</a>( "outermost box" ); s-><a name="setFrameStyle"></a><a href="qframe.html#558f79">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken ); s-><a name="setAlignment"></a><a href="qlabel.html#4743c8">setAlignment</a>( Qt::AlignVCenter | Qt::AlignHCenter ); gm-><a href="qboxlayout.html#ebba99">addWidget</a>( s ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( f ); f-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Custom Layout"); f-><a name="show"></a><a href="qwidget.html#200ee5">show</a>(); int result = a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>(); delete f; return result;}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -