Qt Jambi Home

com.trolltech.qt.gui
Class QBoxLayout

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.gui.QLayout
                  extended by com.trolltech.qt.gui.QBoxLayout
All Implemented Interfaces:
QLayoutItemInterface, QtJambiInterface
Direct Known Subclasses:
QHBoxLayout, QVBoxLayout

public class QBoxLayout
extends QLayout

The QBoxLayout class lines up child widgets horizontally or vertically.

QBoxLayout takes the space it gets (from its parent layout or from the parentWidget), divides it up into a row of boxes, and makes each managed widget fill one box.

Horizontal box layout with five child widgets

If the QBoxLayout's orientation is Qt::Horizontal the boxes are placed in a row, with suitable sizes. Each widget (or other box) will get at least its minimum size and at most its maximum size. Any excess space is shared according to the stretch factors (more about that below).

Vertical box layout with five child widgets

If the QBoxLayout's orientation is Qt::Vertical, the boxes are placed in a column, again with suitable sizes.

The easiest way to create a QBoxLayout is to use one of the convenience classes, e.g. QHBoxLayout (for Qt::Horizontal boxes) or QVBoxLayout (for Qt::Vertical boxes). You can also use the QBoxLayout constructor directly, specifying its direction as LeftToRight, RightToLeft, TopToBottom, or BottomToTop.

If the QBoxLayout is not the top-level layout (i.e. it is not managing all of the widget's area and children), you must add it to its parent layout before you can do anything with it. The normal way to add a layout is by calling parentLayout->addLayout.

Once you have done this, you can add boxes to the QBoxLayout using one of four functions:

Use insertWidget, insertSpacing, insertStretch or insertLayout to insert a box at a specified position in the layout.

QBoxLayout also includes two margin widths:

The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

To remove a widget from a layout, call removeWidget. Calling QWidget::hide() on a widget also effectively removes the widget from the layout until QWidget::show() is called.

You will almost always want to use QVBoxLayout and QHBoxLayout rather than QBoxLayout because of their convenient constructors.

See Also:
QGridLayout, QStackedLayout, Layout Classes

Nested Class Summary
static class QBoxLayout.Direction
          This type is used to determine the direction of a box layout.
 
Nested classes/interfaces inherited from class com.trolltech.qt.gui.QLayout
QLayout.SizeConstraint
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
QBoxLayout(QBoxLayout.Direction arg__1)
          Equivalent to QBoxLayout(arg__1, 0).
QBoxLayout(QBoxLayout.Direction arg__1, QWidget parent)
          Constructs a new QBoxLayout with direction arg__1 and parent widget parent.
 
Method Summary
 void addItem(QLayoutItemInterface arg__1)
          

Implemented in subclasses to add an arg__1.

 void addLayout(QLayout layout)
          Equivalent to addLayout(layout, 0).
 void addLayout(QLayout layout, int stretch)
          Adds layout to the end of the box, with serial stretch factor stretch.
 void addSpacing(int size)
          Adds a non-stretchable space (a QSpacerItem) with size size to the end of this box layout.
 void addStretch()
          Equivalent to addStretch(0).
 void addStretch(int stretch)
          Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.
 void addStrut(int arg__1)
          Limits the perpendicular dimension of the box (e.g. height if the box is LeftToRight) to a minimum of arg__1.
 void addWidget(QWidget arg__1, int stretch, Qt.Alignment alignment)
          Adds arg__1 to the end of this box layout, with a stretch factor of stretch and alignment alignment.
 void addWidget(QWidget arg__1, int stretch, Qt.AlignmentFlag... alignment)
          Adds arg__1 to the end of this box layout, with a stretch factor of stretch and alignment alignment.
 int count()
          

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

 QBoxLayout.Direction direction()
          Returns the direction of the box.
 Qt.Orientations expandingDirections()
          

Returns whether this layout can make use of more space than sizeHint.

static QBoxLayout fromNativePointer(QNativePointer nativePointer)
          This function returns the QBoxLayout instance pointed to by nativePointer
 boolean hasHeightForWidth()
          

Returns true if this layout's preferred height depends on its width; otherwise returns false.

 int heightForWidth(int arg__1)
          

Returns the preferred height for this layout item, given the width arg__1.

protected  void insertItem(int index, QLayoutItemInterface arg__2)
          Inserts arg__2 into this box layout at position index.
 void insertLayout(int index, QLayout layout)
          Equivalent to insertLayout(index, layout, 0).
 void insertLayout(int index, QLayout layout, int stretch)
          Inserts layout at position index, with stretch factor stretch.
 void insertSpacing(int index, int size)
          Inserts a non-stretchable space (a QSpacerItem) at position index, with size size.
 void insertStretch(int index)
          Equivalent to insertStretch(index, 0).
 void insertStretch(int index, int stretch)
          Inserts a stretchable space (a QSpacerItem) at position index, with zero minimum size and stretch factor stretch.
 void insertWidget(int index, QWidget widget)
          Equivalent to insertWidget(index, widget, 0, 0).
 void insertWidget(int index, QWidget widget, int stretch)
          Equivalent to insertWidget(index, widget, stretch, 0).
 void insertWidget(int index, QWidget widget, int stretch, Qt.Alignment alignment)
          Inserts widget at position index, with stretch factor stretch and alignment alignment.
 void insertWidget(int index, QWidget widget, int stretch, Qt.AlignmentFlag... alignment)
          Inserts widget at position index, with stretch factor stretch and alignment alignment.
 void invalidate()
          Resets cached information.
 QLayoutItemInterface itemAt(int arg__1)
          

Must be implemented in subclasses to return the layout item at index.

 QSize maximumSize()
          

Returns the maximum size of this layout.

 int minimumHeightForWidth(int arg__1)
          

Returns the minimum height this widget needs for the given width, arg__1.

 QSize minimumSize()
          

Returns the minimum size of this layout.

 void setDirection(QBoxLayout.Direction arg__1)
          Sets the direction of this layout to arg__1.
 void setGeometry(QRect arg__1)
          

Implemented in subclasses to set this item's geometry to arg__1.

 void setSpacing(int spacing)
          Reimplements QLayout::setSpacing().
 boolean setStretchFactor(QLayout l, int stretch)
          Sets the stretch factor for the layout l to stretch and returns true if l is found in this layout (not including child layouts); otherwise returns false.
 boolean setStretchFactor(QWidget w, int stretch)
          Sets the stretch factor for w to stretch and returns true if w is found in this layout (not including child layouts); otherwise returns false.
 QSize sizeHint()
          

Implemented in subclasses to return the preferred size of this item.

 int spacing()
          Reimplements QLayout::spacing().
 QLayoutItemInterface takeAt(int arg__1)
          

Must be implemented in subclasses to remove the layout item at index from the layout, and return the item.

 
Methods inherited from class com.trolltech.qt.gui.QLayout
activate, addChildLayout, addChildWidget, addWidget, alignment, alignmentRect, childEvent, closestAcceptableSize, contentsRect, controlTypes, geometry, getContentsMargins, indexOf, isEmpty, isEnabled, layout, menuBar, parentWidget, removeItem, removeWidget, setAlignment, setAlignment, setAlignment, setAlignment, setAlignment, setContentsMargins, setContentsMargins, setEnabled, setMargin, setMenuBar, setSizeConstraint, setWidgetSpacing, sizeConstraint, spacerItem, totalHeightForWidth, totalMaximumSize, totalMinimumSize, totalSizeHint, update, widget, widgetEvent, widgetSpacing
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QBoxLayout

public QBoxLayout(QBoxLayout.Direction arg__1)

Equivalent to QBoxLayout(arg__1, 0).


QBoxLayout

public QBoxLayout(QBoxLayout.Direction arg__1,
                  QWidget parent)

Constructs a new QBoxLayout with direction arg__1 and parent widget parent.

See Also:
direction
Method Detail

addLayout

public final void addLayout(QLayout layout)

Equivalent to addLayout(layout, 0).


addLayout

public final void addLayout(QLayout layout,
                            int stretch)

Adds layout to the end of the box, with serial stretch factor stretch.

See Also:
insertLayout, addItem, addWidget

addSpacing

public final void addSpacing(int size)

Adds a non-stretchable space (a QSpacerItem) with size size to the end of this box layout. QBoxLayout provides default margin and spacing. This function adds additional space.

See Also:
insertSpacing, addItem, QSpacerItem

addStretch

public final void addStretch()

Equivalent to addStretch(0).


addStretch

public final void addStretch(int stretch)

Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

See Also:
insertStretch, addItem, QSpacerItem

addStrut

public final void addStrut(int arg__1)

Limits the perpendicular dimension of the box (e.g. height if the box is LeftToRight) to a minimum of arg__1. Other constraints may increase the limit.

See Also:
addItem

addWidget

public final void addWidget(QWidget arg__1,
                            int stretch,
                            Qt.AlignmentFlag... alignment)

Adds arg__1 to the end of this box layout, with a stretch factor of stretch and alignment alignment.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See Also:
insertWidget, addItem, addLayout, addStretch, addSpacing, addStrut

addWidget

public final void addWidget(QWidget arg__1,
                            int stretch,
                            Qt.Alignment alignment)

Adds arg__1 to the end of this box layout, with a stretch factor of stretch and alignment alignment.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See Also:
insertWidget, addItem, addLayout, addStretch, addSpacing, addStrut

direction

public final QBoxLayout.Direction direction()

Returns the direction of the box. addWidget and addSpacing work in this direction; the stretch stretches in this direction.

See Also:
setDirection, QBoxLayout::Direction, addWidget, addSpacing

insertItem

protected final void insertItem(int index,
                                QLayoutItemInterface arg__2)

Inserts arg__2 into this box layout at position index. If index is negative, the item is added at the end.

See Also:
addItem, insertWidget, insertLayout, insertStretch, insertSpacing

insertLayout

public final void insertLayout(int index,
                               QLayout layout)

Equivalent to insertLayout(index, layout, 0).


insertLayout

public final void insertLayout(int index,
                               QLayout layout,
                               int stretch)

Inserts layout at position index, with stretch factor stretch. If index is negative, the layout is added at the end.

layout becomes a child of the box layout.

See Also:
addLayout, insertItem

insertSpacing

public final void insertSpacing(int index,
                                int size)

Inserts a non-stretchable space (a QSpacerItem) at position index, with size size. If index is negative the space is added at the end.

The box layout has default margin and spacing. This function adds additional space.

See Also:
addSpacing, insertItem, QSpacerItem

insertStretch

public final void insertStretch(int index)

Equivalent to insertStretch(index, 0).


insertStretch

public final void insertStretch(int index,
                                int stretch)

Inserts a stretchable space (a QSpacerItem) at position index, with zero minimum size and stretch factor stretch. If index is negative the space is added at the end.

See Also:
addStretch, insertItem, QSpacerItem

insertWidget

public final void insertWidget(int index,
                               QWidget widget,
                               int stretch,
                               Qt.AlignmentFlag... alignment)

Inserts widget at position index, with stretch factor stretch and alignment alignment. If index is negative, the widget is added at the end.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See Also:
addWidget, insertItem

insertWidget

public final void insertWidget(int index,
                               QWidget widget,
                               int stretch)

Equivalent to insertWidget(index, widget, stretch, 0).


insertWidget

public final void insertWidget(int index,
                               QWidget widget)

Equivalent to insertWidget(index, widget, 0, 0).


insertWidget

public final void insertWidget(int index,
                               QWidget widget,
                               int stretch,
                               Qt.Alignment alignment)

Inserts widget at position index, with stretch factor stretch and alignment alignment. If index is negative, the widget is added at the end.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See Also:
addWidget, insertItem

setDirection

public final void setDirection(QBoxLayout.Direction arg__1)

Sets the direction of this layout to arg__1.

See Also:
direction

setSpacing

public final void setSpacing(int spacing)

Reimplements QLayout::setSpacing(). Sets the spacing property to spacing.

See Also:
QLayout::setSpacing, spacing

setStretchFactor

public final boolean setStretchFactor(QLayout l,
                                      int stretch)

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


setStretchFactor

public final boolean setStretchFactor(QWidget w,
                                      int stretch)

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

See Also:
setAlignment

spacing

public final int spacing()

Reimplements QLayout::spacing(). If the spacing property is valid, that value is returned. Otherwise, a value for the spacing property is computed and returned. Since layout spacing in a widget is style dependent, if the parent is a widget, it queries the style for the (horizontal or vertical) spacing of the layout. Otherwise, the parent is a layout, and it queries the parent layout for the spacing.

See Also:
QLayout::spacing, setSpacing

addItem

public void addItem(QLayoutItemInterface arg__1)

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

This function is not usually called in application code. To add a widget to a layout, use the addWidget function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.

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

Specified by:
addItem in class QLayout
See Also:
addWidget, QBoxLayout::addLayout, QGridLayout::addLayout

count

public int count()

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

Specified by:
count in class QLayout
See Also:
itemAt

expandingDirections

public Qt.Orientations expandingDirections()

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.

Specified by:
expandingDirections in interface QLayoutItemInterface
Overrides:
expandingDirections in class QLayout
See Also:
sizeHint

hasHeightForWidth

public boolean hasHeightForWidth()

Returns true if this layout's preferred height depends on its width; otherwise returns false. The default implementation returns false.

Reimplement this function in layout managers that support height for width.

Specified by:
hasHeightForWidth in interface QLayoutItemInterface
Overrides:
hasHeightForWidth in class QLayout
See Also:
heightForWidth, QWidget::heightForWidth

heightForWidth

public int heightForWidth(int arg__1)

Returns the preferred height for this layout item, given the width arg__1.

The default implementation returns -1, indicating that the preferred height is independent of the width of the item. Using the function hasHeightForWidth will typically be much faster than calling this function and testing for -1.

Reimplement this function in layout managers that support height for width. A typical implementation will look like this:

    int MyLayout::heightForWidth(int w) const
    {
        if (cache_dirty || cached_width != w) {
            // not all C++ compilers support "mutable"
            MyLayout *that = (MyLayout*)this;
            int h = calculateHeightForWidth(w);
            that->cached_hfw = h;
            return h;
        }
        return cached_hfw;
    }

Caching is strongly recommended; without it layout will take exponential time.

Specified by:
heightForWidth in interface QLayoutItemInterface
Overrides:
heightForWidth in class QLayout
See Also:
hasHeightForWidth

invalidate

public void invalidate()

Resets cached information.

Specified by:
invalidate in interface QLayoutItemInterface
Overrides:
invalidate in class QLayout

itemAt

public QLayoutItemInterface itemAt(int arg__1)

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(layout->geometry());
    }

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

Specified by:
itemAt in class QLayout
See Also:
count, takeAt

maximumSize

public QSize maximumSize()

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.

Specified by:
maximumSize in interface QLayoutItemInterface
Overrides:
maximumSize in class QLayout

minimumHeightForWidth

public int minimumHeightForWidth(int arg__1)

Returns the minimum height this widget needs for the given width, arg__1. The default implementation simply returns heightForWidth(arg__1).

Specified by:
minimumHeightForWidth in interface QLayoutItemInterface
Overrides:
minimumHeightForWidth in class QLayout

minimumSize

public QSize minimumSize()

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.

Specified by:
minimumSize in interface QLayoutItemInterface
Overrides:
minimumSize in class QLayout

setGeometry

public void setGeometry(QRect arg__1)

Implemented in subclasses to set this item's geometry to arg__1.

Specified by:
setGeometry in interface QLayoutItemInterface
Specified by:
setGeometry in class QLayout
See Also:
geometry

sizeHint

public QSize sizeHint()

Implemented in subclasses to return the preferred size of this item.

Specified by:
sizeHint in interface QLayoutItemInterface
Specified by:
sizeHint in class QLayout

takeAt

public QLayoutItemInterface takeAt(int arg__1)

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;
    }

Specified by:
takeAt in class QLayout
See Also:
itemAt, count

fromNativePointer

public static QBoxLayout fromNativePointer(QNativePointer nativePointer)
This function returns the QBoxLayout instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home