QBoxLayout(3qt)
π
QBoxLayout β the 3qt manual, bestie edition
okay so. welcome to QBoxLayout(3qt), the page that teaches you how to line up your widgets like a perfectly curated Instagram grid. no cap.
β¨ NAME β¨
QBoxLayout β lines up child widgets horizontally or vertically π¦
π SYNOPSIS π
#include <qlayout.h>
Inherits QLayout.
Inherited by QHBoxLayout and QVBoxLayout. (those are the nepo babies, we love them)
π Public Members π
enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop,
Down = TopToBottom, Up = BottomToTop }
QBoxLayout ( QWidget * parent, Direction d, int margin = 0, int spacing
= -1, const char * name = 0 )
QBoxLayout ( QLayout * parentLayout, Direction d, int spacing = -1,
const char * name = 0 )
QBoxLayout ( Direction d, int spacing = -1, const char * name = 0 )
~QBoxLayout ()
virtual void addItem ( QLayoutItem * item )
Direction direction () const
void setDirection ( Direction direction )
void addSpacing ( int size )
void addStretch ( int stretch = 0 )
void addWidget ( QWidget * widget, int stretch = 0, int alignment = 0 )
void addLayout ( QLayout * layout, int stretch = 0 )
void addStrut ( int size )
void insertSpacing ( int index, int size )
void insertStretch ( int index, int stretch = 0 )
void insertWidget ( int index, QWidget * widget, int stretch = 0, int
alignment = 0 )
void insertLayout ( int index, QLayout * layout, int stretch = 0 )
bool setStretchFactor ( QWidget * w, int stretch )
bool setStretchFactor ( QLayout * l, int stretch )
virtual QSize sizeHint () const
virtual QSize minimumSize () const
virtual QSize maximumSize () const
virtual bool hasHeightForWidth () const
virtual int heightForWidth ( int w ) const
virtual QSizePolicy::ExpandData expanding () const
virtual void invalidate ()
virtual void setGeometry ( const QRect & r )
int findWidget ( QWidget * w )
π Protected Members π
void insertItem ( int index, QLayoutItem * item )
πΈ DESCRIPTION πΈ
The QBoxLayout class lines up child widgets horizontally or vertically. that's the whole vibe, that's the whole job π
QBoxLayout takes the space it gets (from its parent layout or from the mainWidget()), divides it up into a row of boxes, and makes each managed widget fill one box. it's giving organizer energy fr fr.
<center> [Image Omitted]
</center>
If the QBoxLayout's orientation is 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). the stretch factors are literally the group-chat drama of space distribution.
<center> [Image Omitted]
</center>
If the QBoxLayout's orientation is Vertical, the boxes are placed in a column, again with suitable sizes. stacked like a cute lil tower π
The easiest way to create a QBoxLayout is to use one of the convenience classes, e.g. QHBoxLayout (for Horizontal boxes) or QVBoxLayout (for Vertical boxes). You can also use the QBoxLayout constructor directly, specifying its direction as LeftToRight, Down, RightToLeft or Up.
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 theQBoxLayoutand 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 anotherQLayoutto 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. yes queen, you can insert anywhere you want π
QBoxLayout also includes two margin widths:
setMargin()sets the width of the outer border. This is the width of the reserved space along each of theQBoxLayout's four sides.setSpacing()sets the width between neighboring boxes. (You can useaddSpacing()to get more space at a particular spot.)
The margin defaults to 0. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout. Both are parameters to the constructor.
To remove a widget from a layout, call remove(). 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. lowkey just use the convenience classes bestie, they're the main characters.
See also QGrid, Layout Overview, Widget Appearance and Style, and Layout Management.
π§ Member Type Documentation π§
QBoxLayout::Direction
This type is used to determine the direction of a box layout.
QBoxLayout::LeftToRightβ Horizontal, from left to rightQBoxLayout::RightToLeftβ Horizontal, from right to leftQBoxLayout::TopToBottomβ Vertical, from top to bottomQBoxLayout::Downβ The same asTopToBottomQBoxLayout::BottomToTopβ Vertical, from bottom to topQBoxLayout::Upβ The same asBottomToTop
π MEMBER FUNCTION DOCUMENTATION π
QBoxLayout::QBoxLayout ( QWidget * parent, Direction d, int margin = 0, int spacing = -1, const char * name = 0 )
Constructs a new QBoxLayout with direction d and main widget parent. parent may not be 0.
The margin is the number of pixels between the edge of the widget and its managed children. The spacing is the default number of pixels between neighboring children. If spacing is -1 the value of margin is used for spacing.
name is the internal object name.
See also direction().
QBoxLayout::QBoxLayout ( QLayout * parentLayout, Direction d, int spacing = -1, const char * name = 0 )
Constructs a new QBoxLayout called name, with direction d, and inserts it into parentLayout.
The spacing is the default number of pixels between neighboring children. If spacing is -1, the layout will inherit its parent's spacing().
QBoxLayout::QBoxLayout ( Direction d, int spacing = -1, const char * name = 0 )
Constructs a new QBoxLayout called name, with direction d.
If spacing is -1, the layout will inherit its parent's spacing(); otherwise spacing is used.
You must insert this box into another layout. fr fr, it cannot stand alone π«Ά
QBoxLayout::~QBoxLayout ()
Destroys this box layout.
The layout's widgets aren't destroyed. okay so the layout dips but the widgets stay, no casualties π
void QBoxLayout::addItem ( QLayoutItem * item ) [virtual]
Adds item to the end of this box layout.
Examples:
Reimplemented from QLayout.
void QBoxLayout::addLayout ( QLayout * layout, int stretch = 0 )
Adds layout to the end of the box, with serial stretch factor stretch.
When a layout is constructed with another layout as its parent, you don't need to call addLayout(); the child layout is automatically added to the parent layout as it is constructed. it just shows up uninvited, iconic behaviour.
See also insertLayout(), setAutoAdd(), addWidget(), and addSpacing().
Examples:
void QBoxLayout::addSpacing ( int size )
Adds a non-stretchable space with size size to the end of this box layout. QBoxLayout provides default margin and spacing. This function adds additional space.
See also insertSpacing() and addStretch().
Example: listbox/listbox.cpp.
void QBoxLayout::addStretch ( int stretch = 0 )
Adds a stretchable space with zero minimum size and stretch factor stretch to the end of this box layout.
See also addSpacing().
Examples:
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.
void QBoxLayout::addWidget ( QWidget * widget, int stretch = 0, int alignment = 0 )
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. the ones with the bigger numbers ate, no crumbs.
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.
Alignment is specified by alignment which is a bitwise OR of Qt::AlignmentFlags values. The default alignment is 0, which means that the widget fills the entire cell.
From Qt 3.0, the alignment parameter is interpreted more aggressively than in previous versions of Qt. A non-default alignment now indicates that the widget should not grow to fill the available space, but should be sized according to sizeHint(). aggressive alignment?? it's giving personal space boundaries, respect them.
See also insertWidget(), setAutoAdd(), addLayout(), and addSpacing().
Examples:
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 QBoxLayout::Direction, addWidget(), and addSpacing().
QSizePolicy::ExpandData QBoxLayout::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, whereas BothDirections means that it wants to grow in both dimensions.
Reimplemented from QLayout.
int QBoxLayout::findWidget ( QWidget * w )
Searches for widget w in this layout (not including child layouts). it's a scavenger hunt, but only on the top level bestie π
Returns the index of w, or -1 if w is not found.
bool QBoxLayout::hasHeightForWidth () const [virtual]
Returns TRUE if this layout's preferred height depends on its width; otherwise returns FALSE.
Reimplemented from QLayoutItem.
int QBoxLayout::heightForWidth ( int w ) const [virtual]
Returns the layout's preferred height when it is w pixels wide.
Reimplemented from QLayoutItem.
void QBoxLayout::insertItem ( int index, QLayoutItem * item ) [protected]
Inserts item into this box layout at position index. If index is negative, the item is added at the end.
β οΈ Warning: Does not callQLayout::insertChildLayout()if item is aQLayout. not me skipping the important part π
See also addItem() and findWidget().
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.
layout becomes a child of the box layout.
See also setAutoAdd(), insertWidget(), and insertSpacing().
void QBoxLayout::insertSpacing ( int index, int size )
Inserts a non-stretchable space 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 insertStretch().
void QBoxLayout::insertStretch ( int index, int stretch = 0 )
Inserts a stretchable space at position index, with zero minimum size and stretch factor stretch. If index is negative the space is added at the end.
See also insertSpacing().
part 1 of 2, don't go anywhere bestie, more coming π¦
β¨ void QBoxLayout::insertWidget ( int index, QWidget * widget, int stretch = 0, int alignment = 0 ) β¨
okay bestie, this one slots a widget in at position index, and you get to bring along a stretch factor and an alignment. and if index is negative?? the widget just gets added at the end, no drama. π
the stretch factor only counts in the direction of the QBoxLayout, and it's measured relative to the other boxes and widgets hanging out in this same QBoxLayout. so like, widgets and boxes with bigger stretch factors grow more. it's giving main character energy for the stretchy ones. π
now if the stretch factor is 0 and literally nothing else in the QBoxLayout has a stretch factor bigger than zero, then the space gets handed out according to the QWidget::sizePolicy() of each widget involved. fair's fair. β¨
alignment comes from alignment, which is a bitwise OR of Qt::AlignmentFlags values. the default alignment is 0, which means the widget fills the entire cell. yes, all of it. π
From Qt 3.0, the alignment parameter is interpreted more aggressively than in previous versions of Qt. a non-default alignment now means the widget should not grow to fill the available space, but should be sized according to sizeHint() instead. no cap. π
also see setAutoAdd(), insertLayout(), and insertSpacing(). π«Ά
π void QBoxLayout::invalidate () [virtual] π
resets cached information. that's it, that's the whole vibe. π§Ή
reimplemented from QLayout.
πΈ QSize QBoxLayout::maximumSize () const [virtual] πΈ
returns the maximum size needed by this box layout. the biggest it could ever be, slay. π₯
reimplemented from QLayout.
π¦ QSize QBoxLayout::minimumSize () const [virtual] π¦
returns the minimum size needed by this box layout. the tiniest it can be and still function. π₯Ί
reimplemented from QLayout.
π void QBoxLayout::setDirection ( Direction direction ) π
sets the direction of this layout to direction. point it where you want, bestie. β‘οΈ
β¨ void QBoxLayout::setGeometry ( const QRect & r ) [virtual] β¨
resizes the managed widgets so they live inside the rectangle r. everyone stays in their lane (the rectangle). π¦
reimplemented from QLayout.
π bool QBoxLayout::setStretchFactor ( QWidget * w, int stretch ) π
sets the stretch factor for widget w to stretch, and returns TRUE if w is found in this layout (not counting child layouts). otherwise it returns FALSE. simple as. π
π«Ά bool QBoxLayout::setStretchFactor ( QLayout * l, int stretch ) π«Ά
this is an overloaded member function, provided for convenience. it behaves essentially like the above function. same energy, different target. β¨
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 it returns FALSE.
β QSize QBoxLayout::sizeHint () const [virtual] β
returns the preferred size of this box layout. the size it wants to be. iconic. π
reimplemented from QLayoutItem.
π SEE ALSO π
π COPYRIGHT π
copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. see the license file included in the distribution for a complete license statement.
βοΈ AUTHOR βοΈ
generated automatically from the source code. (not a human girlie, but we move.) π
π BUGS π
if you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. good bug reports help us to help you. thank you. π«Ά
the definitive Qt documentation is provided in HTML format; it's located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. this man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. so like, keep that in mind, bestie. π
if you find errors in this manual page, please report them to qt-bugs@trolltech.com. please include the name of the manual page (qboxlayout.3qt) and the Qt version (3.3.8). that's how they find you. π¬
and that's a wrap on QBoxLayout(3qt), fr fr. this page was served up by Trolltech AS on 2 February 2007, and the Qt version tagging along is 3.3.8. go forth and lay out your boxes, queen. π
β¨π
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms