1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qboxlayout.h"
43 #include "qapplication.h"
46 #include "qsizepolicy.h"
49 #include "qlayoutengine_p.h"
50 #include "qlayout_p.h"
55 Returns true if the \a widget can be added to the \a layout;
56 otherwise returns false.
58 static bool checkWidget(QLayout *layout, QWidget *widget)
61 qWarning("QLayout: Cannot add null widget to %s/%s", layout->metaObject()->className(),
62 layout->objectName().toLocal8Bit().data());
70 QBoxLayoutItem(QLayoutItem *it, int stretch_ = 0)
71 : item(it), stretch(stretch_), magic(false) { }
72 ~QBoxLayoutItem() { delete item; }
75 if (item->hasHeightForWidth()) {
76 return item->heightForWidth(w);
78 return item->sizeHint().height();
82 if (item->hasHeightForWidth()) {
83 return item->heightForWidth(w);
85 return item->minimumSize().height();
89 if (stretch == 0 && item->widget()) {
90 return item->widget()->sizePolicy().horizontalStretch();
96 if (stretch == 0 && item->widget()) {
97 return item->widget()->sizePolicy().verticalStretch();
108 class QBoxLayoutPrivate : public QLayoutPrivate
110 Q_DECLARE_PUBLIC(QBoxLayout)
112 QBoxLayoutPrivate() : hfwWidth(-1), dirty(true), spacing(-1) { }
113 ~QBoxLayoutPrivate();
122 QList<QBoxLayoutItem *> list;
123 QVector<QLayoutStruct> geomArray;
130 int leftMargin, topMargin, rightMargin, bottomMargin;
131 Qt::Orientations expanding;
134 QBoxLayout::Direction dir;
137 inline void deleteAll() { while (!list.isEmpty()) delete list.takeFirst(); }
142 void effectiveMargins(int *left, int *top, int *right, int *bottom) const;
145 QBoxLayoutPrivate::~QBoxLayoutPrivate()
149 static inline bool horz(QBoxLayout::Direction dir)
151 return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight;
155 * The purpose of this function is to make sure that widgets are not laid out outside its layout.
156 * E.g. the layoutItemRect margins are only meant to take of the surrounding margins/spacings.
157 * However, if the margin is 0, it can easily cover the area of a widget above it.
159 void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *bottom) const
164 int b = bottomMargin;
166 Q_Q(const QBoxLayout);
168 QBoxLayoutItem *leftBox = 0;
169 QBoxLayoutItem *rightBox = 0;
172 leftBox = list.value(0);
173 rightBox = list.value(list.count() - 1);
174 if (dir == QBoxLayout::RightToLeft)
175 qSwap(leftBox, rightBox);
180 QLayoutItem *itm = leftBox->item;
181 if (QWidget *w = itm->widget())
182 leftDelta = itm->geometry().left() - w->geometry().left();
185 QLayoutItem *itm = rightBox->item;
186 if (QWidget *w = itm->widget())
187 rightDelta = w->geometry().right() - itm->geometry().right();
189 QWidget *w = q->parentWidget();
190 Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QApplication::layoutDirection();
191 if (layoutDirection == Qt::RightToLeft)
192 qSwap(leftDelta, rightDelta);
194 l = qMax(l, leftDelta);
195 r = qMax(r, rightDelta);
198 int count = top || bottom ? list.count() : 0;
199 for (int i = 0; i < count; ++i) {
200 QBoxLayoutItem *box = list.at(i);
201 QLayoutItem *itm = box->item;
202 QWidget *w = itm->widget();
204 QRect lir = itm->geometry();
205 QRect wr = w->geometry();
207 t = qMax(t, lir.top() - wr.top());
209 b = qMax(b, wr.bottom() - lir.bottom());
212 } else { // vertical layout
213 QBoxLayoutItem *topBox = 0;
214 QBoxLayoutItem *bottomBox = 0;
217 topBox = list.value(0);
218 bottomBox = list.value(list.count() - 1);
219 if (dir == QBoxLayout::BottomToTop) {
220 qSwap(topBox, bottomBox);
224 QLayoutItem *itm = topBox->item;
225 QWidget *w = itm->widget();
227 t = qMax(t, itm->geometry().top() - w->geometry().top());
230 if (bottom && bottomBox) {
231 QLayoutItem *itm = bottomBox->item;
232 QWidget *w = itm->widget();
234 b = qMax(b, w->geometry().bottom() - itm->geometry().bottom());
238 int count = left || right ? list.count() : 0;
239 for (int i = 0; i < count; ++i) {
240 QBoxLayoutItem *box = list.at(i);
241 QLayoutItem *itm = box->item;
242 QWidget *w = itm->widget();
244 QRect lir = itm->geometry();
245 QRect wr = w->geometry();
247 l = qMax(l, lir.left() - wr.left());
249 r = qMax(r, wr.right() - lir.right());
266 Initializes the data structure needed by qGeomCalc and
267 recalculates max/min and size hint.
269 void QBoxLayoutPrivate::setupGeom()
275 int maxw = horz(dir) ? 0 : QLAYOUTSIZE_MAX;
276 int maxh = horz(dir) ? QLAYOUTSIZE_MAX : 0;
287 int n = list.count();
289 QVector<QLayoutStruct> a(n);
291 QSizePolicy::ControlTypes controlTypes1;
292 QSizePolicy::ControlTypes controlTypes2;
293 int fixedSpacing = q->spacing();
294 int previousNonEmptyIndex = -1;
297 if (fixedSpacing < 0) {
298 if (QWidget *parentWidget = q->parentWidget())
299 style = parentWidget->style();
302 for (int i = 0; i < n; i++) {
303 QBoxLayoutItem *box = list.at(i);
304 QSize max = box->item->maximumSize();
305 QSize min = box->item->minimumSize();
306 QSize hint = box->item->sizeHint();
307 Qt::Orientations exp = box->item->expandingDirections();
308 bool empty = box->item->isEmpty();
312 if (fixedSpacing >= 0) {
313 spacing = (previousNonEmptyIndex >= 0) ? fixedSpacing : 0;
315 if (!horz(dir) && previousNonEmptyIndex >= 0) {
316 QBoxLayoutItem *sibling = (dir == QBoxLayout::TopToBottom ? box : list.at(previousNonEmptyIndex));
318 QWidget *wid = sibling->item->widget();
320 spacing = qMax(spacing, sibling->item->geometry().top() - wid->geometry().top());
325 controlTypes1 = controlTypes2;
326 controlTypes2 = box->item->controlTypes();
327 if (previousNonEmptyIndex >= 0) {
328 QSizePolicy::ControlTypes actual1 = controlTypes1;
329 QSizePolicy::ControlTypes actual2 = controlTypes2;
330 if (dir == QBoxLayout::RightToLeft || dir == QBoxLayout::BottomToTop)
331 qSwap(actual1, actual2);
334 spacing = style->combinedLayoutSpacing(actual1, actual2,
335 horz(dir) ? Qt::Horizontal : Qt::Vertical,
336 0, q->parentWidget());
343 if (previousNonEmptyIndex >= 0)
344 a[previousNonEmptyIndex].spacing = spacing;
345 previousNonEmptyIndex = i;
348 bool ignore = empty && box->item->widget(); // ignore hidden widgets
351 bool expand = (exp & Qt::Horizontal || box->stretch > 0);
352 horexp = horexp || expand;
353 maxw += spacing + max.width();
354 minw += spacing + min.width();
355 hintw += spacing + hint.width();
357 qMaxExpCalc(maxh, verexp, dummy,
358 max.height(), exp & Qt::Vertical, box->item->isEmpty());
359 minh = qMax(minh, min.height());
360 hinth = qMax(hinth, hint.height());
362 a[i].sizeHint = hint.width();
363 a[i].maximumSize = max.width();
364 a[i].minimumSize = min.width();
365 a[i].expansive = expand;
366 a[i].stretch = box->stretch ? box->stretch : box->hStretch();
368 bool expand = (exp & Qt::Vertical || box->stretch > 0);
369 verexp = verexp || expand;
370 maxh += spacing + max.height();
371 minh += spacing + min.height();
372 hinth += spacing + hint.height();
374 qMaxExpCalc(maxw, horexp, dummy,
375 max.width(), exp & Qt::Horizontal, box->item->isEmpty());
376 minw = qMax(minw, min.width());
377 hintw = qMax(hintw, hint.width());
379 a[i].sizeHint = hint.height();
380 a[i].maximumSize = max.height();
381 a[i].minimumSize = min.height();
382 a[i].expansive = expand;
383 a[i].stretch = box->stretch ? box->stretch : box->vStretch();
387 a[i].spacing = 0; // might be initialized with a non-zero value in a later iteration
388 hasHfw = hasHfw || box->item->hasHeightForWidth();
393 expanding = (Qt::Orientations)
394 ((horexp ? Qt::Horizontal : 0)
395 | (verexp ? Qt::Vertical : 0));
397 minSize = QSize(minw, minh);
398 maxSize = QSize(maxw, maxh).expandedTo(minSize);
399 sizeHint = QSize(hintw, hinth).expandedTo(minSize).boundedTo(maxSize);
401 q->getContentsMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin);
402 int left, top, right, bottom;
403 effectiveMargins(&left, &top, &right, &bottom);
404 QSize extra(left + right, top + bottom);
414 Calculates and stores the preferred height given the width \a w.
416 void QBoxLayoutPrivate::calcHfw(int w)
418 QVector<QLayoutStruct> &a = geomArray;
423 Q_ASSERT(n == list.size());
426 qGeomCalc(a, 0, n, 0, w);
427 for (int i = 0; i < n; i++) {
428 QBoxLayoutItem *box = list.at(i);
429 h = qMax(h, box->hfw(a.at(i).size));
430 mh = qMax(mh, box->mhfw(a.at(i).size));
433 for (int i = 0; i < n; ++i) {
434 QBoxLayoutItem *box = list.at(i);
435 int spacing = a.at(i).spacing;
451 \brief The QBoxLayout class lines up child widgets horizontally or
454 \ingroup geomanagement
457 QBoxLayout takes the space it gets (from its parent layout or from
458 the parentWidget()), divides it up into a row of boxes, and makes
459 each managed widget fill one box.
461 \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets
463 If the QBoxLayout's orientation is Qt::Horizontal the boxes are
464 placed in a row, with suitable sizes. Each widget (or other box)
465 will get at least its minimum size and at most its maximum size.
466 Any excess space is shared according to the stretch factors (more
469 \image qvboxlayout-with-5-children.png Vertical box layout with five child widgets
471 If the QBoxLayout's orientation is Qt::Vertical, the boxes are
472 placed in a column, again with suitable sizes.
474 The easiest way to create a QBoxLayout is to use one of the
475 convenience classes, e.g. QHBoxLayout (for Qt::Horizontal boxes)
476 or QVBoxLayout (for Qt::Vertical boxes). You can also use the
477 QBoxLayout constructor directly, specifying its direction as
478 LeftToRight, RightToLeft, TopToBottom, or BottomToTop.
480 If the QBoxLayout is not the top-level layout (i.e. it is not
481 managing all of the widget's area and children), you must add it
482 to its parent layout before you can do anything with it. The
483 normal way to add a layout is by calling
484 parentLayout-\>addLayout().
486 Once you have done this, you can add boxes to the QBoxLayout using
487 one of four functions:
490 \li addWidget() to add a widget to the QBoxLayout and set the
491 widget's stretch factor. (The stretch factor is along the row of
494 \li addSpacing() to create an empty box; this is one of the
495 functions you use to create nice and spacious dialogs. See below
496 for ways to set margins.
498 \li addStretch() to create an empty, stretchable box.
500 \li addLayout() to add a box containing another QLayout to the row
501 and set that layout's stretch factor.
504 Use insertWidget(), insertSpacing(), insertStretch() or
505 insertLayout() to insert a box at a specified position in the
508 QBoxLayout also includes two margin widths:
511 \li setContentsMargins() sets the width of the outer border on
512 each side of the widget. This is the width of the reserved space
513 along each of the QBoxLayout's four sides.
514 \li setSpacing() sets the width between neighboring boxes. (You
515 can use addSpacing() to get more space at a particular spot.)
518 The margin default is provided by the style. The default margin
519 most Qt styles specify is 9 for child widgets and 11 for windows.
520 The spacing defaults to the same as the margin width for a
521 top-level layout, or to the same as the parent layout.
523 To remove a widget from a layout, call removeWidget(). Calling
524 QWidget::hide() on a widget also effectively removes the widget
525 from the layout until QWidget::show() is called.
527 You will almost always want to use QVBoxLayout and QHBoxLayout
528 rather than QBoxLayout because of their convenient constructors.
530 \sa QGridLayout, QStackedLayout, {Layout Management}
534 \enum QBoxLayout::Direction
536 This type is used to determine the direction of a box layout.
538 \value LeftToRight Horizontal from left to right.
539 \value RightToLeft Horizontal from right to left.
540 \value TopToBottom Vertical from top to bottom.
541 \value BottomToTop Vertical from bottom to top.
548 Constructs a new QBoxLayout with direction \a dir and parent widget \a
553 QBoxLayout::QBoxLayout(Direction dir, QWidget *parent)
554 : QLayout(*new QBoxLayoutPrivate, 0, parent)
563 Destroys this box layout.
565 The layout's widgets aren't destroyed.
567 QBoxLayout::~QBoxLayout()
570 d->deleteAll(); // must do it before QObject deletes children, so can't be in ~QBoxLayoutPrivate
574 Reimplements QLayout::spacing(). If the spacing property is
575 valid, that value is returned. Otherwise, a value for the spacing
576 property is computed and returned. Since layout spacing in a widget
577 is style dependent, if the parent is a widget, it queries the style
578 for the (horizontal or vertical) spacing of the layout. Otherwise,
579 the parent is a layout, and it queries the parent layout for the
582 \sa QLayout::spacing(), setSpacing()
584 int QBoxLayout::spacing() const
586 Q_D(const QBoxLayout);
587 if (d->spacing >=0) {
590 return qSmartSpacing(this, d->dir == LeftToRight || d->dir == RightToLeft
591 ? QStyle::PM_LayoutHorizontalSpacing
592 : QStyle::PM_LayoutVerticalSpacing);
597 Reimplements QLayout::setSpacing(). Sets the spacing
598 property to \a spacing.
600 \sa QLayout::setSpacing(), spacing()
602 void QBoxLayout::setSpacing(int spacing)
605 d->spacing = spacing;
612 QSize QBoxLayout::sizeHint() const
614 Q_D(const QBoxLayout);
616 const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
623 QSize QBoxLayout::minimumSize() const
625 Q_D(const QBoxLayout);
627 const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
634 QSize QBoxLayout::maximumSize() const
636 Q_D(const QBoxLayout);
638 const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
640 QSize s = d->maxSize.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX));
642 if (alignment() & Qt::AlignHorizontal_Mask)
643 s.setWidth(QLAYOUTSIZE_MAX);
644 if (alignment() & Qt::AlignVertical_Mask)
645 s.setHeight(QLAYOUTSIZE_MAX);
652 bool QBoxLayout::hasHeightForWidth() const
654 Q_D(const QBoxLayout);
656 const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
663 int QBoxLayout::heightForWidth(int w) const
665 Q_D(const QBoxLayout);
666 if (!hasHeightForWidth())
669 int left, top, right, bottom;
670 d->effectiveMargins(&left, &top, &right, &bottom);
673 if (w != d->hfwWidth)
674 const_cast<QBoxLayout*>(this)->d_func()->calcHfw(w);
676 return d->hfwHeight + top + bottom;
682 int QBoxLayout::minimumHeightForWidth(int w) const
684 Q_D(const QBoxLayout);
685 (void) heightForWidth(w);
687 d->effectiveMargins(0, &top, 0, &bottom);
688 return d->hasHfw ? (d->hfwMinHeight + top + bottom) : -1;
692 Resets cached information.
694 void QBoxLayout::invalidate()
698 QLayout::invalidate();
704 int QBoxLayout::count() const
706 Q_D(const QBoxLayout);
707 return d->list.count();
713 QLayoutItem *QBoxLayout::itemAt(int index) const
715 Q_D(const QBoxLayout);
716 return index >= 0 && index < d->list.count() ? d->list.at(index)->item : 0;
722 QLayoutItem *QBoxLayout::takeAt(int index)
725 if (index < 0 || index >= d->list.count())
727 QBoxLayoutItem *b = d->list.takeAt(index);
728 QLayoutItem *item = b->item;
740 Qt::Orientations QBoxLayout::expandingDirections() const
742 Q_D(const QBoxLayout);
744 const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
751 void QBoxLayout::setGeometry(const QRect &r)
754 if (d->dirty || r != geometry()) {
755 QRect oldRect = geometry();
756 QLayout::setGeometry(r);
759 QRect cr = alignment() ? alignmentRect(r) : r;
761 int left, top, right, bottom;
762 d->effectiveMargins(&left, &top, &right, &bottom);
763 QRect s(cr.x() + left, cr.y() + top,
764 cr.width() - (left + right),
765 cr.height() - (top + bottom));
767 QVector<QLayoutStruct> a = d->geomArray;
768 int pos = horz(d->dir) ? s.x() : s.y();
769 int space = horz(d->dir) ? s.width() : s.height();
771 if (d->hasHfw && !horz(d->dir)) {
772 for (int i = 0; i < n; i++) {
773 QBoxLayoutItem *box = d->list.at(i);
774 if (box->item->hasHeightForWidth()) {
775 int width = qBound(box->item->minimumSize().width(), s.width(), box->item->maximumSize().width());
776 a[i].sizeHint = a[i].minimumSize =
777 box->item->heightForWidth(width);
782 Direction visualDir = d->dir;
783 QWidget *parent = parentWidget();
784 if (parent && parent->isRightToLeft()) {
785 if (d->dir == LeftToRight)
786 visualDir = RightToLeft;
787 else if (d->dir == RightToLeft)
788 visualDir = LeftToRight;
791 qGeomCalc(a, 0, n, pos, space);
793 bool reverse = (horz(visualDir)
794 ? ((r.right() > oldRect.right()) != (visualDir == RightToLeft))
795 : r.bottom() > oldRect.bottom());
796 for (int j = 0; j < n; j++) {
797 int i = reverse ? n-j-1 : j;
798 QBoxLayoutItem *box = d->list.at(i);
802 box->item->setGeometry(QRect(a.at(i).pos, s.y(), a.at(i).size, s.height()));
805 box->item->setGeometry(QRect(s.left() + s.right() - a.at(i).pos - a.at(i).size + 1,
806 s.y(), a.at(i).size, s.height()));
809 box->item->setGeometry(QRect(s.x(), a.at(i).pos, s.width(), a.at(i).size));
812 box->item->setGeometry(QRect(s.x(),
813 s.top() + s.bottom() - a.at(i).pos - a.at(i).size + 1,
814 s.width(), a.at(i).size));
823 void QBoxLayout::addItem(QLayoutItem *item)
826 QBoxLayoutItem *it = new QBoxLayoutItem(item);
832 Inserts \a item into this box layout at position \a index. If \a
833 index is negative, the item is added at the end.
835 \sa addItem(), insertWidget(), insertLayout(), insertStretch(),
838 void QBoxLayout::insertItem(int index, QLayoutItem *item)
841 if (index < 0) // append
842 index = d->list.count();
844 QBoxLayoutItem *it = new QBoxLayoutItem(item);
845 d->list.insert(index, it);
850 Inserts a non-stretchable space (a QSpacerItem) at position \a index, with
851 size \a size. If \a index is negative the space is added at the end.
853 The box layout has default margin and spacing. This function adds
856 \sa addSpacing(), insertItem(), QSpacerItem
858 void QBoxLayout::insertSpacing(int index, int size)
861 if (index < 0) // append
862 index = d->list.count();
866 b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum);
868 b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Minimum, QSizePolicy::Fixed);
871 QBoxLayoutItem *it = new QBoxLayoutItem(b);
873 d->list.insert(index, it);
883 Inserts a stretchable space (a QSpacerItem) at position \a
884 index, with zero minimum size and stretch factor \a stretch. If \a
885 index is negative the space is added at the end.
887 \sa addStretch(), insertItem(), QSpacerItem
889 void QBoxLayout::insertStretch(int index, int stretch)
892 if (index < 0) // append
893 index = d->list.count();
897 b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
899 b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
901 QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch);
903 d->list.insert(index, it);
910 Inserts \a spacerItem at position \a index, with zero minimum
911 size and stretch factor. If \a index is negative the
912 space is added at the end.
914 \sa addSpacerItem(), insertStretch(), insertSpacing()
916 void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem)
919 if (index < 0) // append
920 index = d->list.count();
922 QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem);
924 d->list.insert(index, it);
929 Inserts \a layout at position \a index, with stretch factor \a
930 stretch. If \a index is negative, the layout is added at the end.
932 \a layout becomes a child of the box layout.
934 \sa addLayout(), insertItem()
936 void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch)
939 addChildLayout(layout);
940 if (index < 0) // append
941 index = d->list.count();
942 QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch);
943 d->list.insert(index, it);
948 Inserts \a widget at position \a index, with stretch factor \a
949 stretch and alignment \a alignment. If \a index is negative, the
950 widget is added at the end.
952 The stretch factor applies only in the \l{direction()}{direction}
953 of the QBoxLayout, and is relative to the other boxes and widgets
954 in this QBoxLayout. Widgets and boxes with higher stretch factors
957 If the stretch factor is 0 and nothing else in the QBoxLayout has
958 a stretch factor greater than zero, the space is distributed
959 according to the QWidget:sizePolicy() of each widget that's
962 The alignment is specified by \a alignment. The default alignment
963 is 0, which means that the widget fills the entire cell.
965 \sa addWidget(), insertItem()
967 void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch,
968 Qt::Alignment alignment)
971 if (!checkWidget(this, widget))
973 addChildWidget(widget);
974 if (index < 0) // append
975 index = d->list.count();
976 QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget);
977 b->setAlignment(alignment);
981 it = new QBoxLayoutItem(b, stretch);
988 d->list.insert(index, it);
997 Adds a non-stretchable space (a QSpacerItem) with size \a size
998 to the end of this box layout. QBoxLayout provides default margin
999 and spacing. This function adds additional space.
1001 \sa insertSpacing(), addItem(), QSpacerItem
1003 void QBoxLayout::addSpacing(int size)
1005 insertSpacing(-1, size);
1009 Adds a stretchable space (a QSpacerItem) with zero minimum
1010 size and stretch factor \a stretch to the end of this box layout.
1012 \sa insertStretch(), addItem(), QSpacerItem
1014 void QBoxLayout::addStretch(int stretch)
1016 insertStretch(-1, stretch);
1022 Adds \a spacerItem to the end of this box layout.
1024 \sa addSpacing(), addStretch()
1026 void QBoxLayout::addSpacerItem(QSpacerItem *spacerItem)
1028 insertSpacerItem(-1, spacerItem);
1032 Adds \a widget to the end of this box layout, with a stretch
1033 factor of \a stretch and alignment \a alignment.
1035 The stretch factor applies only in the \l{direction()}{direction}
1036 of the QBoxLayout, and is relative to the other boxes and widgets
1037 in this QBoxLayout. Widgets and boxes with higher stretch factors
1040 If the stretch factor is 0 and nothing else in the QBoxLayout has
1041 a stretch factor greater than zero, the space is distributed
1042 according to the QWidget:sizePolicy() of each widget that's
1045 The alignment is specified by \a alignment. The default
1046 alignment is 0, which means that the widget fills the entire cell.
1048 \sa insertWidget(), addItem(), addLayout(), addStretch(),
1049 addSpacing(), addStrut()
1051 void QBoxLayout::addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
1053 insertWidget(-1, widget, stretch, alignment);
1057 Adds \a layout to the end of the box, with serial stretch factor
1060 \sa insertLayout(), addItem(), addWidget()
1062 void QBoxLayout::addLayout(QLayout *layout, int stretch)
1064 insertLayout(-1, layout, stretch);
1068 Limits the perpendicular dimension of the box (e.g. height if the
1069 box is \l LeftToRight) to a minimum of \a size. Other constraints
1070 may increase the limit.
1074 void QBoxLayout::addStrut(int size)
1079 b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Fixed, QSizePolicy::Minimum);
1081 b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Minimum, QSizePolicy::Fixed);
1083 QBoxLayoutItem *it = new QBoxLayoutItem(b);
1090 Sets the stretch factor for \a widget to \a stretch and returns
1091 true if \a widget is found in this layout (not including child
1092 layouts); otherwise returns false.
1096 bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch)
1101 for (int i = 0; i < d->list.size(); ++i) {
1102 QBoxLayoutItem *box = d->list.at(i);
1103 if (box->item->widget() == widget) {
1104 box->stretch = stretch;
1115 Sets the stretch factor for the layout \a layout to \a stretch and
1116 returns true if \a layout is found in this layout (not including
1117 child layouts); otherwise returns false.
1119 bool QBoxLayout::setStretchFactor(QLayout *layout, int stretch)
1122 for (int i = 0; i < d->list.size(); ++i) {
1123 QBoxLayoutItem *box = d->list.at(i);
1124 if (box->item->layout() == layout) {
1125 if (box->stretch != stretch) {
1126 box->stretch = stretch;
1136 Sets the stretch factor at position \a index. to \a stretch.
1141 void QBoxLayout::setStretch(int index, int stretch)
1144 if (index >= 0 && index < d->list.size()) {
1145 QBoxLayoutItem *box = d->list.at(index);
1146 if (box->stretch != stretch) {
1147 box->stretch = stretch;
1154 Returns the stretch factor at position \a index.
1159 int QBoxLayout::stretch(int index) const
1161 Q_D(const QBoxLayout);
1162 if (index >= 0 && index < d->list.size())
1163 return d->list.at(index)->stretch;
1168 Sets the direction of this layout to \a direction.
1170 void QBoxLayout::setDirection(Direction direction)
1173 if (d->dir == direction)
1175 if (horz(d->dir) != horz(direction)) {
1176 //swap around the spacers (the "magic" bits)
1177 //#### a bit yucky, knows too much.
1178 //#### probably best to add access functions to spacerItem
1179 //#### or even a QSpacerItem::flip()
1180 for (int i = 0; i < d->list.size(); ++i) {
1181 QBoxLayoutItem *box = d->list.at(i);
1183 QSpacerItem *sp = box->item->spacerItem();
1185 if (sp->expandingDirections() == Qt::Orientations(0) /*No Direction*/) {
1187 QSize s = sp->sizeHint();
1188 sp->changeSize(s.height(), s.width(),
1189 horz(direction) ? QSizePolicy::Fixed:QSizePolicy::Minimum,
1190 horz(direction) ? QSizePolicy::Minimum:QSizePolicy::Fixed);
1194 if (horz(direction))
1195 sp->changeSize(0, 0, QSizePolicy::Expanding,
1196 QSizePolicy::Minimum);
1198 sp->changeSize(0, 0, QSizePolicy::Minimum,
1199 QSizePolicy::Expanding);
1210 \fn QBoxLayout::Direction QBoxLayout::direction() const
1212 Returns the direction of the box. addWidget() and addSpacing()
1213 work in this direction; the stretch stretches in this direction.
1215 \sa QBoxLayout::Direction, addWidget(), addSpacing()
1218 QBoxLayout::Direction QBoxLayout::direction() const
1220 Q_D(const QBoxLayout);
1226 \brief The QHBoxLayout class lines up widgets horizontally.
1228 \ingroup geomanagement
1231 This class is used to construct horizontal box layout objects. See
1232 QBoxLayout for details.
1234 The simplest use of the class is like this:
1236 \snippet layouts/layouts.cpp 0
1237 \snippet layouts/layouts.cpp 1
1238 \snippet layouts/layouts.cpp 2
1240 \snippet layouts/layouts.cpp 3
1241 \snippet layouts/layouts.cpp 4
1242 \snippet layouts/layouts.cpp 5
1244 First, we create the widgets we want in the layout. Then, we
1245 create the QHBoxLayout object and add the widgets into the
1246 layout. Finally, we call QWidget::setLayout() to install the
1247 QHBoxLayout object onto the widget. At that point, the widgets in
1248 the layout are reparented to have \c window as their parent.
1250 \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets
1252 \sa QVBoxLayout, QGridLayout, QStackedLayout, {Layout Management}, {Basic Layouts Example}
1257 Constructs a new top-level horizontal box with
1260 QHBoxLayout::QHBoxLayout(QWidget *parent)
1261 : QBoxLayout(LeftToRight, parent)
1266 Constructs a new horizontal box. You must add
1267 it to another layout.
1269 QHBoxLayout::QHBoxLayout()
1270 : QBoxLayout(LeftToRight)
1279 Destroys this box layout.
1281 The layout's widgets aren't destroyed.
1283 QHBoxLayout::~QHBoxLayout()
1289 \brief The QVBoxLayout class lines up widgets vertically.
1291 \ingroup geomanagement
1294 This class is used to construct vertical box layout objects. See
1295 QBoxLayout for details.
1297 The simplest use of the class is like this:
1299 \snippet layouts/layouts.cpp 6
1300 \snippet layouts/layouts.cpp 7
1301 \snippet layouts/layouts.cpp 8
1303 \snippet layouts/layouts.cpp 9
1304 \snippet layouts/layouts.cpp 10
1305 \snippet layouts/layouts.cpp 11
1307 First, we create the widgets we want in the layout. Then, we
1308 create the QVBoxLayout object and add the widgets into the
1309 layout. Finally, we call QWidget::setLayout() to install the
1310 QVBoxLayout object onto the widget. At that point, the widgets in
1311 the layout are reparented to have \c window as their parent.
1313 \image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets
1315 \sa QHBoxLayout, QGridLayout, QStackedLayout, {Layout Management}, {Basic Layouts Example}
1319 Constructs a new top-level vertical box with
1322 QVBoxLayout::QVBoxLayout(QWidget *parent)
1323 : QBoxLayout(TopToBottom, parent)
1328 Constructs a new vertical box. You must add
1329 it to another layout.
1332 QVBoxLayout::QVBoxLayout()
1333 : QBoxLayout(TopToBottom)
1339 Destroys this box layout.
1341 The layout's widgets aren't destroyed.
1343 QVBoxLayout::~QVBoxLayout()