Qt 4.2: QLayout Class Reference

|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QLayout Class Reference
[QtGui module]

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

     #include <QLayout>

Inherits QObject and QLayoutItem.

Inherited by QBoxLayout, QGridLayout, and QStackedLayout.

- List of all members, including inherited members
- Qt 3 support members

### Public Types

- enum SizeConstraint { SetDefaultConstraint, SetFixedSize, SetMinimumSize, SetMaximumSize, SetMinAndMaxSize, SetNoConstraint }

### Properties

- margin : int
- sizeConstraint : SizeConstraint
- spacing : int

- 1 property inherited from QObject

### Public Functions

- QLayout ( QWidget * parent )
- QLayout ()
- bool activate ()
- virtual void addItem ( QLayoutItem * item ) = 0
- void addWidget ( QWidget * w )
- virtual int count () const = 0
- virtual Qt::Orientations expandingDirections () const
- virtual int indexOf ( QWidget * widget ) const
- bool isEnabled () const
- virtual QLayoutItem * itemAt ( int index ) const = 0
- int margin () const
- virtual QSize maximumSize () const
- QWidget * menuBar () const
- virtual QSize minimumSize () const
- QWidget * parentWidget () const
- void removeItem ( QLayoutItem * item )
- void removeWidget ( QWidget * widget )
- bool setAlignment ( QWidget * w, Qt::Alignment alignment )
- bool setAlignment ( QLayout * l, Qt::Alignment alignment )
- void setEnabled ( bool enable )
- void setMargin ( int )
- void setMenuBar ( QWidget * widget )
- void setSizeConstraint ( SizeConstraint )
- void setSpacing ( int )
- SizeConstraint sizeConstraint () const
- int spacing () const
- virtual QLayoutItem * takeAt ( int index ) = 0
- void update ()

- 29 public functions inherited from QObject
- 16 public functions inherited from QLayoutItem

### Static Public Members

- QSize closestAcceptableSize ( const QWidget * widget, const QSize & size )

- 5 static public members inherited from QObject

### Protected Functions

- void addChildLayout ( QLayout * l )
- void addChildWidget ( QWidget * w )
- QRect alignmentRect ( const QRect & r ) const

- 7 protected functions inherited from QObject

### Additional Inherited Members

- 1 public slot inherited from QObject
- 1 signal inherited from QObject

## Detailed Description

The QLayout class is the base class of geometry managers.

This is an abstract base class inherited by the concrete classes QBoxLayout, QGridLayout, and QStackedLayout.

For users of QLayout subclasses or of QMainWindow there is seldom any need to use the basic functions provided by QLayout, such as setSizeConstraint() or setMenuBar(). See Layout Classes for more information.

To make your own layout manager, implement the functions addItem(), sizeHint(), setGeometry(), itemAt() and takeAt(). You should also implement minimumSize() to ensure your layout isn't resized to zero size if there is too little space. To support children whose heights depend on their widths, implement hasHeightForWidth() and heightForWidth(). See the Border Layout and Flow Layout examples for more information about implementing custom layout managers.

Geometry management stops when the layout manager is deleted.

See also QLayoutItem, Layout Classes, Basic Layouts Example, Border Layout Example, and Flow Layout Example.

## Member Type Documentation

### enum QLayout::SizeConstraint

The possible values are:

Constant | Value | Description
--- | --- | ---
`QLayout::SetDefaultConstraint` | `0` | The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size.
`QLayout::SetFixedSize` | `3` | The main widget's size is set to sizeHint(); it cannot be resized at all.
`QLayout::SetMinimumSize` | `2` | The main widget's minimum size is set to minimumSize(); it cannot be smaller.
`QLayout::SetMaximumSize` | `4` | The main widget's maximum size is set to maximumSize(); it cannot be larger.
`QLayout::SetMinAndMaxSize` | `5` | The main widget's minimum size is set to minimumSize() and its maximum size is set to maximumSize().
`QLayout::SetNoConstraint` | `1` | The widget is not constrained.

See also setSizeConstraint().

## Property Documentation

### margin : int

This property holds the width of the outside border of the layout.

The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows.

Access functions:

- int margin () const
- void setMargin ( int )

See also spacing.

### sizeConstraint : SizeConstraint

This property holds the resize mode of the layout.

The default mode is SetDefaultConstraint.

Access functions:

- SizeConstraint sizeConstraint () const
- void setSizeConstraint ( SizeConstraint )

### spacing : int

This property holds the spacing between widgets inside the layout.

The default value is -1, which signifies that the layout's spacing is inherited from the parent layout, or from the style settings for the parent widget.

Access functions:

- int spacing () const
- void setSpacing ( int )

See also margin.

## Member Function Documentation

### QLayout::QLayout ( QWidget * parent )

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

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

### QLayout::QLayout ()

Constructs a new child QLayout.

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

### bool QLayout::activate ()

Redoes the layout for parentWidget() if necessary.

You should generally not need to call this because it is automatically called at the most appropriate times.

See also update() and QWidget::updateGeometry().

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

This function is called from `addLayout()` functions in subclasses to add layout l as a sub-layout.

### void QLayout::addChildWidget ( QWidget * w ) `[protected]`

This function is called from `addWidget()` functions in subclasses to add w as a child widget.

If w is already in a layout, this function will give a warning and remove w from the layout. This function must therefore be called before adding w to the layout's data structure.

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

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

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

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

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

### 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, provided that this layout supports setAlignment().

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

### QSize QLayout::closestAcceptableSize ( const QWidget * widget, const QSize & size ) `[static]`

Returns a size that satisfies all size constraints on widget, including heightForWidth() and that is as close as possible to size.

### int QLayout::count () const `[pure virtual]`

Must be implemented in subclasses to return the number of items in the layout.

See also itemAt().

### Qt::Orientations QLayout::expandingDirections () const `[virtual]`

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

The default implementation returns Qt::Horizontal | Qt::Vertical. Subclasses reimplement it to return a meaningful value based on their child widgets's size policies.

Reimplemented from QLayoutItem.

See also sizeHint().

### int QLayout::indexOf ( QWidget * widget ) const `[virtual]`

Searches for widget widget in this layout (not including child layouts).

Returns the index of widget, or -1 if widget is not found.

The default implementation iterates over all items using itemAt()

### bool QLayout::isEnabled () const

Returns true if the layout is enabled; otherwise returns false.

See also setEnabled().

### QLayoutItem * QLayout::itemAt ( int index ) const `[pure virtual]`

Must be implemented in subclasses to return the layout item at index. If there is no such item, the function must return 0. Items are numbered consecutively from 0. If an item is deleted, other items will be renumbered.

This function can be used to iterate over a layout. The following code will draw a rectangle for each layout item in the layout structure of the widget.

     static void paintLayout(QPainter *painter, QLayoutItem *item)
     {
         QLayout *layout = item->layout();
         if (layout) {
             for (int i = 0; i < layout->count(); ++i)
                 paintLayout(painter, layout->itemAt(i));
         }
         painter->drawRect(lay->geometry());
     }

     void MyWidget::paintEvent(QPaintEvent *)
     {
         QPainter painter(this);
         if (layout())
             paintLayout(&painter, layout());
     }

See also count() and takeAt().

### 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.

The returned value doesn't include the space required by QWidget::setContentsMargins() or menuBar().

The default implementation allows unlimited resizing.

Reimplemented from QLayoutItem.

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

Returns the menu bar set for this layout, or 0 if no menu bar is set.

See also setMenuBar().

### 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.

The returned value doesn't include the space required by QWidget::setContentsMargins() or menuBar().

The default implementation allows unlimited resizing.

Reimplemented from QLayoutItem.

### QWidget * QLayout::parentWidget () const

Returns the parent widget of this layout, or 0 if this layout is not installed on any widget.

If the layout is a sub-layout, this function returns the parent widget of the parent layout.

See also parent().

### void QLayout::removeItem ( QLayoutItem * item )

Removes the layout item item from the layout. It is the caller's responsibility to delete the item.

Notice that item can be a layout (since QLayout inherits QLayoutItem).

See also removeWidget() and addItem().

### void QLayout::removeWidget ( QWidget * widget )

Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.

See also removeItem(), QWidget::setGeometry(), and addWidget().

### bool QLayout::setAlignment ( QWidget * w, Qt::Alignment alignment )

Sets the alignment for widget w to alignment and returns true if w is found in this layout (not including child layouts); otherwise returns false.

### bool QLayout::setAlignment ( QLayout * l, Qt::Alignment alignment )

This is an overloaded member function, provided for convenience.

Sets the alignment for the layout l to alignment and returns true if l is found in this layout (not including child layouts); otherwise returns false.

### 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 did not exist.

By default all layouts are enabled.

See also isEnabled().

### void QLayout::setMenuBar ( QWidget * widget )

Tells the geometry manager to place the menu bar widget at the top of parentWidget(), outside QWidget::contentsMargins(). All child widgets are placed below the bottom edge of the menu bar.

See also menuBar().

### QLayoutItem * QLayout::takeAt ( int index ) `[pure virtual]`

Must be implemented in subclasses to remove the layout item at index from the layout, and return the item. If there is no such item, the function must do nothing and return 0. Items are numbered consecutively from 0. If an item is deleted, other items will be renumbered.

The following code fragment shows a safe way to remove all items from a layout:

     QLayoutItem *child;
     while ((child = layout->takeAt(0)) != 0) {
         ...
         delete child;
     }

See also itemAt() and count().

### void QLayout::update ()

Updates the layout for parentWidget().

You should generally not need to call this because it is automatically called at the most appropriate times.

See also activate() and invalidate().

## Member Function Documentation

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

Constructs a new top-level QLayout called name, with parent widget parent. parent may not be 0.

The margin is the number of pixels between the edge of the widget and the managed children. The spacing sets the value of spacing(), which gives the spacing between the managed widgets. If spacing is -1 (the default), spacing is set to the value of margin.

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

See also QWidget::setLayout().

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

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

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

### QLayout::QLayout ( int spacing, const char * name = 0 )

Constructs a new child QLayout called name. If spacing is -1, this QLayout inherits its parent's spacing(); otherwise the value of spacing is used.

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

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

Use addWidget(widget) instead.

### bool QLayout::autoAdd () const

Automatically adding widgets is deprecated. Use addWidget() or addLayout() instead.

See also setAutoAdd().

### int QLayout::defaultBorder () const

Use spacing() instead.

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

Removes and deletes all items in this layout.

### void QLayout::freeze ( int w = 0, int h = 0 )

Sets this layout's parent widget to a fixed size with width w and height h, stopping the user form resizing it, and also prevents the layout from resizing it, even if the layout's size hint should change. Does nothing if this is not a top-level layout (i.e., if parent()->isWidgetType()).

As a special case, if both w and h are 0, then the layout's current sizeHint() is used.

Use `setResizeMode(Fixed)` to stop the widget from being resized by the user, while still allowing the layout to resize it when the sizeHint() changes.

Use `setResizeMode(FreeResize)` to allow the user to resize the widget, while preventing the layout from resizing it.

### bool QLayout::isTopLevel () const

Returns true if this layout is a top-level layout, i.e. not a child of another layout; otherwise returns false.

### QLayoutIterator QLayout::iterator ()

Use a QLayoutIterator() constructor instead.

### QWidget * QLayout::mainWidget () const

Use parentWidget() instead.

### void QLayout::remove ( QWidget * widget )

Use removeWidget(widget) instead.

### SizeConstraint QLayout::resizeMode () const

Use sizeConstraint() instead.

See also setResizeMode().

### void QLayout::setAutoAdd ( bool a )

Automatically adding widgets is deprecated. Use addWidget() or addLayout() instead.

See also autoAdd().

### void QLayout::setResizeMode ( SizeConstraint constraint )

Use setSizeConstraint(constraint) instead.

See also resizeMode().