📄 qboxlayout.cpp
字号:
\o addStretch() to create an empty, stretchable box. \o addLayout() to add a box containing another QLayout to the row and set that layout's stretch factor. \endlist Use insertWidget(), insertSpacing(), insertStretch() or insertLayout() to insert a box at a specified position in the layout. QBoxLayout also includes two margin widths: \list \o setMargin() sets the width of the outer border. This is the width of the reserved space along each of the QBoxLayout's four sides. \o setSpacing() sets the width between neighboring boxes. (You can use addSpacing() to get more space at a particular spot.) \endlist 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. \sa QGridLayout, QStackedLayout, {Layout Classes}*//*! \enum QBoxLayout::Direction This type is used to determine the direction of a box layout. \value LeftToRight Horizontal from left to right. \value RightToLeft Horizontal from right to left. \value TopToBottom Vertical from top to bottom. \value BottomToTop Vertical from bottom to top. \omitvalue Down \omitvalue Up*//*! Constructs a new QBoxLayout with direction \a dir and parent widget \a parent. \sa direction()*/QBoxLayout::QBoxLayout(Direction dir, QWidget *parent) : QLayout(*new QBoxLayoutPrivate, 0, parent){ Q_D(QBoxLayout); d->dir = dir;}#ifdef QT3_SUPPORT/*! Constructs a new QBoxLayout with direction \a dir and main widget \a parent. \a parent may not be 0. The \a margin is the number of pixels between the edge of the widget and its managed children. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1 the value of \a margin is used for \a spacing. \a name is the internal object name. \sa direction()*/QBoxLayout::QBoxLayout(QWidget *parent, Direction dir, int margin, int spacing, const char *name) : QLayout(*new QBoxLayoutPrivate, 0, parent){ Q_D(QBoxLayout); d->dir = dir; setMargin(margin); setObjectName(QString::fromAscii(name)); setSpacing(spacing<0 ? margin : spacing);}/*! Constructs a new QBoxLayout called \a name, with direction \a dir, and inserts it into \a parentLayout. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1, the layout will inherit its parent's spacing().*/QBoxLayout::QBoxLayout(QLayout *parentLayout, Direction dir, int spacing, const char *name) : QLayout(*new QBoxLayoutPrivate, parentLayout, 0){ Q_D(QBoxLayout); d->dir = dir; setObjectName(QString::fromAscii(name)); setSpacing(spacing);}/*! Constructs a new QBoxLayout called \a name, with direction \a dir. If \a spacing is -1, the layout will inherit its parent's spacing(); otherwise \a spacing is used. You must insert this box into another layout.*/QBoxLayout::QBoxLayout(Direction dir, int spacing, const char *name) : QLayout(*new QBoxLayoutPrivate,0, 0){ Q_D(QBoxLayout); d->dir = dir; setObjectName(QString::fromAscii(name)); setSpacing(spacing);}#endif // QT3_SUPPORT/*! Destroys this box layout. The layout's widgets aren't destroyed.*/QBoxLayout::~QBoxLayout(){ Q_D(QBoxLayout); d->deleteAll(); // must do it before QObject deletes children, so can't be in ~QBoxLayoutPrivate}/*! 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(). \sa QLayout::spacing(), setSpacing() */int QBoxLayout::spacing() const{ Q_D(const QBoxLayout); if (d->spacing >=0) { return d->spacing; } else { return qSmartSpacing(this, d->dir == LeftToRight || d->dir == RightToLeft ? QStyle::PM_LayoutHorizontalSpacing : QStyle::PM_LayoutVerticalSpacing); }}/*! Reimplements QLayout::setSpacing(). Sets the spacing property to \a spacing. \sa QLayout::setSpacing(), spacing() */void QBoxLayout::setSpacing(int spacing){ Q_D(QBoxLayout); d->spacing = spacing; invalidate();}/*! \reimp*/QSize QBoxLayout::sizeHint() const{ Q_D(const QBoxLayout); if (d->dirty) const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); return d->sizeHint;}/*! \reimp*/QSize QBoxLayout::minimumSize() const{ Q_D(const QBoxLayout); if (d->dirty) const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); return d->minSize;}/*! \reimp*/QSize QBoxLayout::maximumSize() const{ Q_D(const QBoxLayout); if (d->dirty) const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); QSize s = d->maxSize; s = s.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX)); if (alignment() & Qt::AlignHorizontal_Mask) s.setWidth(QLAYOUTSIZE_MAX); if (alignment() & Qt::AlignVertical_Mask) s.setHeight(QLAYOUTSIZE_MAX); return s;}/*! \reimp*/bool QBoxLayout::hasHeightForWidth() const{ Q_D(const QBoxLayout); if (d->dirty) const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); return d->hasHfw;}/*! \reimp*/int QBoxLayout::heightForWidth(int w) const{ Q_D(const QBoxLayout); if (!hasHeightForWidth()) return -1; int left, top, right, bottom; d->effectiveMargins(&left, &top, &right, &bottom); w -= left + right; if (w != d->hfwWidth) const_cast<QBoxLayout*>(this)->d_func()->calcHfw(w); return d->hfwHeight + top + bottom;}/*! \reimp*/int QBoxLayout::minimumHeightForWidth(int w) const{ Q_D(const QBoxLayout); (void) heightForWidth(w); int top, bottom; d->effectiveMargins(0, &top, 0, &bottom); return d->hasHfw ? (d->hfwMinHeight + top + bottom) : -1;}/*! Resets cached information.*/void QBoxLayout::invalidate(){ Q_D(QBoxLayout); d->setDirty(); QLayout::invalidate();}/*! \reimp*/int QBoxLayout::count() const{ Q_D(const QBoxLayout); return d->list.count();}/*! \reimp*/QLayoutItem *QBoxLayout::itemAt(int index) const{ Q_D(const QBoxLayout); return index >= 0 && index < d->list.count() ? d->list.at(index)->item : 0;}/*! \reimp*/QLayoutItem *QBoxLayout::takeAt(int index){ Q_D(QBoxLayout); if (index < 0 || index >= d->list.count()) return 0; QBoxLayoutItem *b = d->list.takeAt(index); QLayoutItem *item = b->item; b->item = 0; delete b; invalidate(); return item;}/*! \reimp*/Qt::Orientations QBoxLayout::expandingDirections() const{ Q_D(const QBoxLayout); if (d->dirty) const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); return d->expanding;}/*! \reimp*/void QBoxLayout::setGeometry(const QRect &r){ Q_D(QBoxLayout); if (d->dirty || r != geometry()) { QRect oldRect = geometry(); QLayout::setGeometry(r); if (d->dirty) d->setupGeom(); QRect cr = alignment() ? alignmentRect(r) : r; int left, top, right, bottom; d->effectiveMargins(&left, &top, &right, &bottom); QRect s(cr.x() + left, cr.y() + top, cr.width() - (left + right), cr.height() - (top + bottom)); QVector<QLayoutStruct> a = d->geomArray; int pos = horz(d->dir) ? s.x() : s.y(); int space = horz(d->dir) ? s.width() : s.height(); int n = a.count(); if (d->hasHfw && !horz(d->dir)) { for (int i = 0; i < n; i++) { QBoxLayoutItem *box = d->list.at(i); if (box->item->hasHeightForWidth()) a[i].sizeHint = a[i].minimumSize = box->item->heightForWidth(s.width()); } } Direction visualDir = d->dir; QWidget *parent = parentWidget(); if (parent && parent->isRightToLeft()) { if (d->dir == LeftToRight) visualDir = RightToLeft; else if (d->dir == RightToLeft) visualDir = LeftToRight; } qGeomCalc(a, 0, n, pos, space); bool reverse = (horz(visualDir) ? ((r.right() > oldRect.right()) != (visualDir == RightToLeft)) : r.bottom() > oldRect.bottom()); for (int j = 0; j < n; j++) { int i = reverse ? n-j-1 : j; QBoxLayoutItem *box = d->list.at(i); switch (visualDir) { case LeftToRight: box->item->setGeometry(QRect(a.at(i).pos, s.y(), a.at(i).size, s.height())); break; case RightToLeft: box->item->setGeometry(QRect(s.left() + s.right() - a.at(i).pos - a.at(i).size + 1, s.y(), a.at(i).size, s.height())); break; case TopToBottom: box->item->setGeometry(QRect(s.x(), a.at(i).pos, s.width(), a.at(i).size)); break; case BottomToTop: box->item->setGeometry(QRect(s.x(), s.top() + s.bottom() - a.at(i).pos - a.at(i).size + 1, s.width(), a.at(i).size)); } } }}/*! \reimp*/void QBoxLayout::addItem(QLayoutItem *item){ Q_D(QBoxLayout); QBoxLayoutItem *it = new QBoxLayoutItem(item); d->list.append(it); invalidate();}/*! Inserts \a item into this box layout at position \a index. If \a index is negative, the item is added at the end. \sa addItem(), insertWidget(), insertLayout(), insertStretch(), insertSpacing()*/void QBoxLayout::insertItem(int index, QLayoutItem *item){ Q_D(QBoxLayout); if (index < 0) // append index = d->list.count(); QBoxLayoutItem *it = new QBoxLayoutItem(item); d->list.insert(index, it); invalidate();}/*! Inserts a non-stretchable space (a QSpacerItem) at position \a index, with size \a size. If \a index is negative the space is added at the end. The box layout has default margin and spacing. This function adds additional space. \sa addSpacing(), insertItem(), QSpacerItem*/void QBoxLayout::insertSpacing(int index, int size){ Q_D(QBoxLayout); if (index < 0) // append index = d->list.count(); QLayoutItem *b; if (horz(d->dir)) b = new QSpacerItem(size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum); else b = new QSpacerItem(0, size, QSizePolicy::Minimum, QSizePolicy::Fixed); QBoxLayoutItem *it = new QBoxLayoutItem(b); it->magic = true; d->list.insert(index, it); invalidate();}/*! Inserts a stretchable space (a QSpacerItem) at position \a index, with zero minimum size and stretch factor \a stretch. If \a index is negative the space is added at the end. \sa addStretch(), insertItem(), QSpacerItem*/void QBoxLayout::insertStretch(int index, int stretch){ Q_D(QBoxLayout); if (index < 0) // append index = d->list.count(); QLayoutItem *b; if (horz(d->dir)) b = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); else b = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding); QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch); it->magic = true; d->list.insert(index, it); invalidate();}/*! Inserts \a layout at position \a index, with stretch factor \a stretch. If \a index is negative, the layout is added at the end. \a layout becomes a child of the box layout. \sa addLayout(), insertItem()*/void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch){ Q_D(QBoxLayout); addChildLayout(layout); if (index < 0) // append index = d->list.count(); QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch); d->list.insert(index, it); invalidate();}/*! Inserts \a widget at position \a index, with stretch factor \a stretch and alignment \a alignment. If \a index is negative, the widget is added at the end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -