Customized LayoutmanagerThis examples demonstrates how to write customized layout (geometry) managers like card layouts, border layout and flow layouts. See also: Documentation of Geometry Management.
Header file of the flow layout:
/**************************************************************************** ** $Id: $ ** ** 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 <qlayout.h> #include <qptrlist.h> class SimpleFlow : public QLayout { public: SimpleFlow( QWidget *parent, int border=0, int space=-1, const char *name=0 ) : QLayout( parent, border, space, name ), cached_width(0) {} SimpleFlow( QLayout* parent, int space=-1, const char *name=0 ) : QLayout( parent, space, name ), cached_width(0) {} SimpleFlow( int space=-1, const char *name=0 ) : QLayout( space, name ), cached_width(0) {} ~SimpleFlow(); void addItem( QLayoutItem *item); bool hasHeightForWidth() const; int heightForWidth( int ) const; QSize sizeHint() const; QSize minimumSize() const; QLayoutIterator iterator(); QSizePolicy::ExpandData expanding() const; protected: void setGeometry( const QRect& ); private: int doLayout( const QRect&, bool testonly = FALSE ); QPtrList<QLayoutItem> list; int cached_width; int cached_hfw; }; #endif
Implementation of the flow layout:
/**************************************************************************** ** $Id: $ ** ** 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( QPtrList<QLayoutItem> *l ) :idx(0), list(l) {} uint count() const; QLayoutItem *current(); QLayoutItem *next(); QLayoutItem *takeCurrent(); private: int idx; QPtrList<QLayoutItem> *list; }; uint
Header file of the border layout:
/**************************************************************************** ** $Id: $ ** ** 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 <qlayout.h> #include <qptrlist.h> class BorderWidgetItem : public QWidgetItem { public: BorderWidgetItem( QWidget *w ) : QWidgetItem( 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( QLayoutItem *i, Position p ) { item = i; pos = p; } QLayoutItem *item; Position pos; }; enum SizeType { Minimum = 0, SizeHint }; BorderLayout( QWidget *parent, int border = 0, int autoBorder = -1, const char *name = 0 ) : QLayout( parent, border, autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ), sizeDirty( TRUE ), msizeDirty( TRUE ) {} BorderLayout( QLayout* parent, int autoBorder = -1, const char *name = 0 ) : QLayout( parent, autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ), sizeDirty( TRUE ), msizeDirty( TRUE ) {} BorderLayout( int autoBorder = -1, const char *name = 0 ) : QLayout( autoBorder, name ), cached( 0, 0 ), mcached( 0, 0 ), sizeDirty( TRUE ), msizeDirty( TRUE ) {} ~BorderLayout(); void addItem( QLayoutItem *item ); void addWidget( QWidget *widget, Position pos ); void add( QLayoutItem *item, Position pos ); bool hasHeightForWidth() const; QSize sizeHint() const; QSize minimumSize() const; QLayoutIterator iterator(); QSizePolicy::ExpandData expanding() const; protected: void setGeometry( const QRect &rect ); private: void doLayout( const QRect &rect, bool testonly = FALSE ); void calcSize( SizeType st ); QPtrList<BorderLayoutStruct> list; QSize cached, mcached; bool sizeDirty, msizeDirty; }; #endif
Implementation of the border layout:
/**************************************************************************** ** $Id: $ ** ** 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 QPtrList<BorderLayout::BorderLayoutStruct> *l ) : idx( 0 ) , list( (QPtrList<BorderLayout::BorderLayoutStruct>*)l ) {} uint count() const; QLayoutItem *current(); BorderLayout::BorderLayoutStruct *currentStruct(); void toFirst(); QLayoutItem *next(); QLayoutItem *takeCurrent(); BorderLayoutIterator &operator++(); private: int idx; QPtrList<BorderLayout::BorderLayoutStruct> *list; }; uint
Header file of the card layout:
/**************************************************************************** ** $Id: $ ** ** 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 <qlayout.h> #include <qptrlist.h> class CardLayout : public QLayout { public: CardLayout( QWidget *parent, int dist ) : QLayout( parent, 0, dist ) {} CardLayout( QLayout* parent, int dist) : QLayout( parent, dist ) {} CardLayout( int dist ) : QLayout( dist ) {} ~CardLayout(); void addItem( QLayoutItem *item ); QSize sizeHint() const; QSize minimumSize() const; QLayoutIterator iterator(); void setGeometry( const QRect &rect ); private: QPtrList<QLayoutItem> list; }; #endif
Implementation of the card layout:
/**************************************************************************** ** $Id: $ ** ** 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( QPtrList<QLayoutItem> *l ) : idx( 0 ), list( l ) {} QLayoutItem *current(); QLayoutItem *next(); QLayoutItem *takeCurrent(); private: int idx; QPtrList<QLayoutItem> *list; };
Main:
/**************************************************************************** ** $Id: $ ** ** 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 <qapplication.h> #include <qlabel.h> #include <qcolor.h> #include <qgroupbox.h> #include <qpushbutton.h> #include <qmultilineedit.h> #include <qcolor.h> int main( int argc, char **argv ) { QApplication a( argc, argv ); QWidget *f = new QWidget; QBoxLayout *gm = new QVBoxLayout( f, 5 ); SimpleFlow *b1 = new SimpleFlow( gm ); See also Examples. |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com