CopperSpice API  1.7.2
QBoxLayout Class Reference

The QBoxLayout class lines up child widgets horizontally or vertically. More...

Inheritance diagram for QBoxLayout:
QLayout QObject QLayoutItem QHBoxLayout QVBoxLayout

Public Types

enum  Direction
 
- Public Types inherited from QLayout
enum  SizeConstraint
 

Public Methods

 QBoxLayout (Direction direction, QWidget *parent=nullptr)
 
 ~QBoxLayout ()
 
void addItem (QLayoutItem *item) override
 
void addLayout (QLayout *layout, int stretch=0)
 
void addSpacerItem (QSpacerItem *spacerItem)
 
void addSpacing (int size)
 
void addStretch (int stretch=0)
 
void addStrut (int size)
 
void addWidget (QWidget *widget, int stretch=0, Qt::Alignment alignment=Qt::Alignment ())
 
int count () const override
 
Direction direction () const
 
Qt::Orientations expandingDirections () const override
 
bool hasHeightForWidth () const override
 
int heightForWidth (int width) const override
 
void insertItem (int index, QLayoutItem *item)
 
void insertLayout (int index, QLayout *layout, int stretch=0)
 
void insertSpacerItem (int index, QSpacerItem *spacerItem)
 
void insertSpacing (int index, int size)
 
void insertStretch (int index, int stretch=0)
 
void insertWidget (int index, QWidget *widget, int stretch=0, Qt::Alignment alignment=Qt::Alignment ())
 
void invalidate () override
 
QLayoutItemitemAt (int index) const override
 
QSize maximumSize () const override
 
int minimumHeightForWidth (int width) const override
 
QSize minimumSize () const override
 
void setDirection (Direction direction)
 
void setGeometry (const QRect &rect) override
 
void setSpacing (int spacing)
 
void setStretch (int index, int stretch)
 
bool setStretchFactor (QLayout *layout, int stretch)
 
bool setStretchFactor (QWidget *widget, int stretch)
 
QSize sizeHint () const override
 
int spacing () const
 
int stretch (int index) const
 
QLayoutItemtakeAt (int index) override
 
- Public Methods inherited from QLayout
 QLayout ()
 
 QLayout (QWidget *parent)
 
bool activate ()
 
void addWidget (QWidget *w)
 
QMargins contentsMargins () const
 
QRect contentsRect () const
 
QSizePolicy::ControlTypes controlTypes () const override
 
Qt::Orientations expandingDirections () const override
 
QRect geometry () const override
 
void getContentsMargins (int *left, int *top, int *right, int *bottom) const
 
virtual int indexOf (QWidget *widget) const
 
void invalidate () override
 
bool isEmpty () const override
 
bool isEnabled () const
 
QLayout * layout () override
 
int margin () const
 
QSize maximumSize () const override
 
QWidgetmenuBar () const
 
QSize minimumSize () const override
 
QWidgetparentWidget () const
 
void removeItem (QLayoutItem *item)
 
void removeWidget (QWidget *widget)
 
virtual QLayoutItemreplaceWidget (QWidget *from, QWidget *to, Qt::FindChildOptions options=Qt::FindChildrenRecursively)
 
bool setAlignment (QLayout *layout, Qt::Alignment alignment)
 
void setAlignment (Qt::Alignment alignment)
 
bool setAlignment (QWidget *widget, Qt::Alignment alignment)
 
void setContentsMargins (const QMargins &margins)
 
void setContentsMargins (int left, int top, int right, int bottom)
 
void setEnabled (bool enable)
 
void setGeometry (const QRect &rect) override
 
void setMargin (int margin)
 
void setMenuBar (QWidget *widget)
 
void setSizeConstraint (SizeConstraint constraint)
 
void setSpacing (int spacing)
 
SizeConstraint sizeConstraint () const
 
int spacing () const
 
void update ()
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 
- Public Methods inherited from QLayoutItem
 QLayoutItem (Qt::Alignment alignment=Qt::Alignment ())
 
virtual ~QLayoutItem ()
 
Qt::Alignment alignment () const
 
void setAlignment (Qt::Alignment alignment)
 
virtual QSpacerItemspacerItem ()
 
virtual QWidgetwidget ()
 

Additional Inherited Members

- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QLayout
static QSize closestAcceptableSize (const QWidget *widget, const QSize &size)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QLayout
void addChildLayout (QLayout *layout)
 
void addChildWidget (QWidget *widget)
 
QRect alignmentRect (const QRect &rect) const
 
void childEvent (QChildEvent *event) override
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QLayout
 margin
 
 sizeConstraint
 
 spacing
 
- Properties inherited from QObject
 objectName
 

Detailed Description

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.

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

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:

  • addWidget() to add a widget to the QBoxLayout and set the widget's stretch factor. (The stretch factor is along the row of boxes.)
  • addSpacing() to create an empty box; this is one of the functions you use to create nice and spacious dialogs. See below for ways to set margins.
  • addStretch() to create an empty, stretchable box.
  • addLayout() to add a box containing another QLayout to the row and set that layout's stretch factor.

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

QBoxLayout also includes two margin widths:

  • setContentsMargins() sets the width of the outer border on each side of the widget. This is the width of the reserved space along each of the QBoxLayout's four sides.
  • setSpacing() sets the width between neighboring boxes. (You can use addSpacing() to get more space at a particular spot.)

The margin default is provided by the style. The default margin most CopperSpice 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 Management

Member Enumeration Documentation

This type is used to determine the direction of a box layout.

ConstantValueDescription
QBoxLayout::LeftToRight0Horizontal from left to right.
QBoxLayout::RightToLeft1Horizontal from right to left.
QBoxLayout::TopToBottom2Vertical from top to bottom.
QBoxLayout::BottomToTop3Vertical from bottom to top.

Constructor & Destructor Documentation

QBoxLayout::QBoxLayout ( Direction  direction,
QWidget parent = nullptr 
)
explicit

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

See also
direction()
QBoxLayout::~QBoxLayout ( )

Destroys this box layout. The layout's widgets are not destroyed.

Method Documentation

void QBoxLayout::addItem ( QLayoutItem item)
overridevirtual

Reimplemented from QLayout::addItem().

Implements QLayout.

void QBoxLayout::addLayout ( QLayout layout,
int  stretch = 0 
)

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

See also
insertLayout(), addItem(), addWidget()
void QBoxLayout::addSpacerItem ( QSpacerItem spacerItem)

Adds spacerItem to the end of this box layout.

See also
addSpacing(), addStretch()
void QBoxLayout::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
void QBoxLayout::addStretch ( int  stretch = 0)

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
void QBoxLayout::addStrut ( int  size)

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

See also
addItem()
void QBoxLayout::addWidget ( QWidget widget,
int  stretch = 0,
Qt::Alignment  alignment = Qt::Alignment() 
)

Adds widget 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()
int QBoxLayout::count ( ) const
overridevirtual

Reimplemented from QLayout::count().

Implements QLayout.

Direction QBoxLayout::direction ( ) const

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()
Qt::Orientations QBoxLayout::expandingDirections ( ) const
overridevirtual

Reimplemented from QLayoutItem::expandingDirections().

Implements QLayoutItem.

bool QBoxLayout::hasHeightForWidth ( ) const
overridevirtual

Reimplemented from QLayoutItem::hasHeightForWidth().

Reimplemented from QLayoutItem.

int QBoxLayout::heightForWidth ( int  width) const
overridevirtual

Reimplemented from QLayoutItem::heightForWidth().

Reimplemented from QLayoutItem.

void QBoxLayout::insertItem ( int  index,
QLayoutItem item 
)

Inserts item 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()
void QBoxLayout::insertLayout ( int  index,
QLayout layout,
int  stretch = 0 
)

Inserts layout at position index, with stretch factor stretch. If index is negative, the layout is added at the end. The layout becomes a child of the box layout.

See also
addLayout(), insertItem()
void QBoxLayout::insertSpacerItem ( int  index,
QSpacerItem spacerItem 
)

Inserts spacerItem at position index, with zero minimum size and stretch factor. If index is negative the space is added at the end.

See also
addSpacerItem(), insertStretch(), insertSpacing()
void QBoxLayout::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
void QBoxLayout::insertStretch ( int  index,
int  stretch = 0 
)

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
void QBoxLayout::insertWidget ( int  index,
QWidget widget,
int  stretch = 0,
Qt::Alignment  alignment = Qt::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()
void QBoxLayout::invalidate ( )
overridevirtual

Reimplemented from QLayoutItem::invalidate().

Resets cached information.

Reimplemented from QLayoutItem.

QLayoutItem * QBoxLayout::itemAt ( int  index) const
overridevirtual

Reimplemented from QLayout::itemAt().

Implements QLayout.

QSize QBoxLayout::maximumSize ( ) const
overridevirtual

Reimplemented from QLayoutItem::maximumSize().

Implements QLayoutItem.

int QBoxLayout::minimumHeightForWidth ( int  width) const
overridevirtual

Reimplemented from QLayoutItem::minimumHeightForWidth().

Reimplemented from QLayoutItem.

QSize QBoxLayout::minimumSize ( ) const
overridevirtual

Reimplemented from QLayoutItem::minimumSize().

Implements QLayoutItem.

void QBoxLayout::setDirection ( Direction  direction)

Sets the direction of this layout to direction.

See also
direction()
void QBoxLayout::setGeometry ( const QRect rect)
overridevirtual

Reimplemented from QLayoutItem::setGeometry().

Implements QLayoutItem.

void QBoxLayout::setSpacing ( int  spacing)

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

See also
QLayout::setSpacing(), spacing()
void QBoxLayout::setStretch ( int  index,
int  stretch 
)

Sets the stretch factor at position index. to stretch.

See also
stretch()
bool QBoxLayout::setStretchFactor ( QLayout layout,
int  stretch 
)

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

bool QBoxLayout::setStretchFactor ( QWidget widget,
int  stretch 
)

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

See also
setAlignment()
QSize QBoxLayout::sizeHint ( ) const
overridevirtual

Reimplemented from QLayoutItem::sizeHint().

Implements QLayoutItem.

int QBoxLayout::spacing ( ) const

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()
int QBoxLayout::stretch ( int  index) const

Returns the stretch factor at position index.

See also
setStretch()
QLayoutItem * QBoxLayout::takeAt ( int  index)
overridevirtual

Reimplemented from QLayout::takeAt().

Implements QLayout.