Qt Toolkit - QLayout Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QLayout Class Reference

The QLayout class is the base class of geometry specifiers. More...

`#include <qabstractlayout.h>`

Inherits QLayoutItem and QObject.

Inherited by QBoxLayout and QGridLayout.

List of all member functions.

## Public Members

- QLayout ( QWidget * parent, int margin=0, int space=-1, const char * name=0 )
- QLayout ( QLayout * parentLayout, int space=-1, const char * name=0 )
- QLayout ( int space=-1, const char * name=0 )
- ~QLayout ()
- int margin () const
- int spacing () const
- virtual void setMargin ( int )
- virtual void setSpacing ( int )
- enum { unlimited = QWIDGETSIZE_MAX }
- enum ResizeMode { FreeResize, Minimum, Fixed }
- void setResizeMode ( ResizeMode )
- ResizeMode resizeMode () const
- virtual void setMenuBar ( QMenuBar * w )
- QMenuBar* menuBar () const
- QWidget* mainWidget ()
- bool isTopLevel () const
- virtual void setAutoAdd ( bool )
- bool autoAdd () const
- virtual void invalidate ()
- bool activate ()
- void add ( QWidget * w )
- virtual void addItem ( QLayoutItem * )
- virtual QSizePolicy::ExpandData expanding () const
- virtual QSize minimumSize () const
- virtual QSize maximumSize () const
- virtual void setGeometry ( const QRect & )
- virtual QLayoutIterator iterator ()
- virtual bool isEmpty () const
- int totalHeightForWidth ( int w ) const (internal)
- QSize totalMinimumSize () const (internal)
- QSize totalMaximumSize () const (internal)
- QSize totalSizeHint () const (internal)
- bool supportsMargin () const
- void setEnabled ( bool )
- bool isEnabled () const

## Protected Members

- virtual bool eventFilter ( QObject *, QEvent * )
- void addChildLayout ( QLayout * l )
- void deleteAllItems ()
- void setSupportsMargin ( bool )
- QRect alignmentRect ( const QRect & ) const

## Properties

Type | Name | READ | WRITE | Options
--- | --- | --- | --- | ---
int | margin | margin | setMargin |
int | spacing | spacing | setSpacing |
ResizeMode | resizeMode | resizeMode | setResizeMode |

## Detailed Description

The QLayout class is the base class of geometry specifiers.

This is an abstract base class. The concrete layout managers QBoxLayout and QGridLayout inherit from this one.

For users of Q*Layout or QMainWindow, there is seldom need to use any of the basic functions provided by QLayout, such as setResizeMode() or setMenuBar(). See the layout overview page for more information.

To make your own layout manager, make a subclass of QGLayoutIterator and implement the functions addItem(), sizeHint(), setGeometry() and iterator(). You should also implement minimumSize(), otherwise your layout will be resized to zero size if there is little space. To support children whose height depend on their widths, implement hasHeightForWidth() and heightForWidth(). See the custom layout page for an in-depth description.

Geometry management stops when the layout manager is deleted.

## Member Type Documentation

### QLayout::ResizeMode

The possible values are are:

- `Fixed` - the main widget's size is set to sizeHint(), it cannot be resized at all.
- `Minimum` - The main widget's minimum size is set to minimumSize(), it cannot be smaller.
- `FreeResize` - the widget is not constrained.

## Member Function Documentation

### QLayout::QLayout ( QWidget * parent, int border=0, int space=-1, const char * name=0 )

Constructs a new top-level QLayout with main widget parent. parent may not be 0.

border is the number of pixels between the edge of the widget and the managed children. space sets the value of spacing(), which gives the spacing between widgets. The default value for space is -1, which means that the value of border is used.

name is the internal object name

There can only be one top-level layout for a widget. It is returned by QWidget::layout().

### QLayout::QLayout ( QLayout * parentLayout, int space=-1, const char * name=0 )

Constructs a new child QLayout, and places it inside parentLayout, using the default placement defined by addItem().

If space is -1, this QLayout inherits parentLayout's spacing(), otherwise space is used.

### QLayout::QLayout ( int space=-1, const char * name=0 )

Constructs a new child QLayout, If space is -1, this QLayout inherits its parent's spacing(), otherwise space is used.

This layout has to be inserted into another layout before geometry management will work.

### QLayout::~QLayout ()

Destructs the layout, deleting all child layouts. Geometry management stops when a toplevel layout is deleted.

### bool QLayout::activate ()

Redoes the layout for mainWidget(). You should generally not need to call this, as it is automatically called at most appropriate times.

However, if you set up a QLayout for a visible widget without resizing that widget, you need to call this function in order to lay it out.

See also QWidget::updateGeometry().

Examples: layout/layout.cpp popup/popup.cpp scrollview/scrollview.cpp

### void QLayout::add ( QWidget * w )

Adds w to this layout in a manner specific to the layout. This function uses addItem.

Examples: customlayout/main.cpp

### void QLayout::addChildLayout ( QLayout * l ) `[protected]`

This function is called from addLayout functions in subclasses, to add l layout as a sublayout.

### void QLayout::addItem ( QLayoutItem * item ) `[virtual]`

Implemented in subclasses to add item. How it is added is specific to each subclass.

Note that the ownership of item is transferred to the layout, and it is the layout's responsibility to delete it.

Reimplemented in QGridLayout and QBoxLayout.

### QRect QLayout::alignmentRect ( const QRect & r ) const `[protected]`

Returns the rectangle that should be covered when the geometry of this layout is set to r, if this layout supports setAlignment().

The result is calculated from sizeHint() and expanding(). It is never larger than r.

### bool QLayout::autoAdd () const

Returns TRUE if this layout automatically grabs all new mainWidget()'s new children and adds them as defined by addItem(). This only has effect for top-level layouts, ie. layouts that are direct children of their mainWidget().

autoAdd() is disabled by default.

See also setAutoAdd().

### void QLayout::deleteAllItems () `[protected]`

Removes and deletes all items in this layout.

### bool QLayout::eventFilter ( QObject * o, QEvent * e ) `[virtual protected]`

Performs child widget layout when the parent widget is resized. Also handles removal of widgets and child layouts.

Reimplemented from QObject.

### QSizePolicy::ExpandData QLayout::expanding() const `[virtual]`

Returns whether this layout can make use of more space than sizeHint(). A value of Vertical or Horizontal means that it wants to grow in only one dimension, while BothDirections means that it wants to grow in both dimensions.

The default implementation returns BothDirections.

Reimplemented from QLayoutItem.

### QRect QLayout::geometry () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QLayoutItem.

### void QLayout::invalidate () `[virtual]`

Invalidates cached information. Reimplementations must call this.

Reimplemented from QLayoutItem.

### bool QLayout::isEmpty () const `[virtual]`

Returns TRUE if this layout is empty. The default implementation returns FALSE.

Reimplemented from QLayoutItem.

### bool QLayout::isEnabled () const

Returns whether or not this layout is enabled.

See also setEnabled().

### bool QLayout::isTopLevel () const

Returns TRUE if this layout is a top level layout, i.e. not a child of another layout.

### QLayoutIterator QLayout::iterator () `[virtual]`

Returns an iterator over the children of this layout.

This function must be implemented by subclasses.

Reimplemented from QLayoutItem.

### QLayout * QLayout::layout () `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QLayoutItem.

### QWidget * QLayout::mainWidget ()

Returns the main widget (parent widget) of this layout, or 0 if this layout is a sub-layout which is not yet inserted.

### int QLayout::margin () const

returns the width of the outside border of the layout.

See also spacing() and setMargin().

### QSize QLayout::maximumSize () const `[virtual]`

Returns the maximum size of this layout. This is the largest size that the layout can have, while still respecting the specifications. Does not include what's needed by margin() or menuBar().

The default implementation allows unlimited resizing.

Reimplemented from QLayoutItem.

### QMenuBar* QLayout::menuBar () const

Returns the menu bar set for this layout, or a null pointer if no menu bar is set.

### QSize QLayout::minimumSize () const `[virtual]`

Returns the minimum size of this layout. This is the smallest size that the layout can have, while still respecting the specifications. Does not include what's needed by margin() or menuBar().

The default implementation allows unlimited resizing.

Reimplemented from QLayoutItem.

### QLayout::ResizeMode QLayout::resizeMode() const

Returns the resize mode.

### void QLayout::setAutoAdd ( bool b ) `[virtual]`

Sets autoAdd() if b is TRUE.

See also autoAdd().

Examples: i18n/main.cpp

### void QLayout::setEnabled ( bool enable )

Enables this layout if enable is TRUE, otherwise disables it.

An enabled layout adjusts dynamically to changes, a disabled layout acts as if it was not existing.

By default, all layouts are enabled.

See also isEnabled().

### void QLayout::setGeometry ( const QRect & r ) `[virtual]`

This function is reimplemented in subclasses to perform layout.

The default implementation maintains the geometry() information. Reimplementors must call this function.

Reimplemented from QLayoutItem.

### void QLayout::setMargin ( int border ) `[virtual]`

Sets the outside border of the layout to border.

For some layout classes, this function only has an effect on top-level layouts; QBoxLayout and QGridLayout support margins for child layouts.

For some layout classes, this function only has an effect on top-level layouts; QBoxLayout and QGridLayout support margins for child layouts.

See also margin(), setSpacing() and supportsMargin().

### void QLayout::setMenuBar ( QMenuBar * w ) `[virtual]`

Makes the geometry manager take account of the menu bar w. All child widgets are placed below the bottom edge of the menu bar.

A menu bar does its own geometry managing, never do addWidget() on a QMenuBar.

Examples: scrollview/scrollview.cpp

### void QLayout::setResizeMode ( ResizeMode mode )

Sets the resize mode to mode.

The default mode is `Minimum` for top level widgets, and `FreeResize` for all others.

See also QLayout::ResizeMode.

### void QLayout::setSpacing ( int space ) `[virtual]`

Sets the internal spacing of the layout to space.

See also spacing() and setMargin().

### void QLayout::setSupportsMargin ( bool b ) `[protected]`

Sets the value returned by supportsMargin(). If b is TRUE, margin() handling is implemented by the subclass. If b is FALSE (the default) QLayout will add margin() around top-level layouts.

If b is TRUE, margin handling needs to be implemented in setGeometry(), maximumSize(), minimumSize(), sizeHint() and heightForWidth().

See also supportsMargin().

### int QLayout::spacing () const

Returns the spacing between widgets inside the layout.

See also margin() and setSpacing().

### bool QLayout::supportsMargin () const

Returns TRUE if this layout supports setMargin() on non-toplevel layouts.

See also setMargin().

### int QLayout::totalHeightForWidth ( int w ) const

For internal use only.

### QSize QLayout::totalMaximumSize () const

For internal use only.

### QSize QLayout::totalMinimumSize () const

For internal use only.

### QSize QLayout::totalSizeHint () const

For internal use only.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.