📄 qboxlayout.cpp
字号:
The stretch factor applies only in the \l{direction()}{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 \a alignment. The default alignment is 0, which means that the widget fills the entire cell. \sa addWidget(), insertItem()*/void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch, Qt::Alignment alignment){ Q_D(QBoxLayout); if (!checkWidget(this, widget)) return; addChildWidget(widget); if (index < 0) // append index = d->list.count(); QWidgetItem *b = new QWidgetItem(widget); b->setAlignment(alignment); QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch); d->list.insert(index, it); invalidate();}/*! Adds a non-stretchable space (a QSpacerItem) with size \a size to the end of this box layout. QBoxLayout provides default margin and spacing. This function adds additional space. \sa insertSpacing(), addItem(), QSpacerItem*/void QBoxLayout::addSpacing(int size){ insertSpacing(-1, size);}/*! Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor \a stretch to the end of this box layout. \sa insertStretch(), addItem(), QSpacerItem*/void QBoxLayout::addStretch(int stretch){ insertStretch(-1, stretch);}/*! Adds \a widget to the end of this box layout, with a stretch factor of \a stretch and alignment \a alignment. The stretch factor applies only in the \l{direction()}{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 \a alignment. The default alignment is 0, which means that the widget fills the entire cell. \sa insertWidget(), addItem(), addLayout(), addStretch(), addSpacing(), addStrut()*/void QBoxLayout::addWidget(QWidget *widget, int stretch, Qt::Alignment alignment){ insertWidget(-1, widget, stretch, alignment);}/*! Adds \a layout to the end of the box, with serial stretch factor \a stretch. \sa insertLayout(), addItem(), addWidget()*/void QBoxLayout::addLayout(QLayout *layout, int stretch){ insertLayout(-1, layout, stretch);}/*! Limits the perpendicular dimension of the box (e.g. height if the box is \l LeftToRight) to a minimum of \a size. Other constraints may increase the limit. \sa addItem()*/void QBoxLayout::addStrut(int size){ Q_D(QBoxLayout); QLayoutItem *b; if (horz(d->dir)) b = new QSpacerItem(0, size, QSizePolicy::Fixed, QSizePolicy::Minimum); else b = new QSpacerItem(size, 0, QSizePolicy::Minimum, QSizePolicy::Fixed); QBoxLayoutItem *it = new QBoxLayoutItem(b); it->magic = true; d->list.append(it); invalidate();}/*! \fn int QBoxLayout::findWidget(QWidget *widget) Use indexOf(\a widget) instead.*//*! Sets the stretch factor for \a widget to \a stretch and returns true if \a widget is found in this layout (not including child layouts); otherwise returns false. \sa setAlignment()*/bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch){ Q_D(QBoxLayout); for (int i = 0; i < d->list.size(); ++i) { QBoxLayoutItem *box = d->list.at(i); if (box->item->widget() == widget) { box->stretch = stretch; invalidate(); return true; } } return false;}/*! \overload Sets the stretch factor for the layout \a layout to \a stretch and returns true if \a layout is found in this layout (not including child layouts); otherwise returns false.*/bool QBoxLayout::setStretchFactor(QLayout *layout, int stretch){ Q_D(QBoxLayout); for (int i = 0; i < d->list.size(); ++i) { QBoxLayoutItem *box = d->list.at(i); if (box->item->layout() == layout) { box->stretch = stretch; invalidate(); return true; } } return false;}/*! Sets the direction of this layout to \a direction.*/void QBoxLayout::setDirection(Direction direction){ Q_D(QBoxLayout); if (d->dir == direction) return; if (horz(d->dir) != horz(direction)) { //swap around the spacers (the "magic" bits) //#### a bit yucky, knows too much. //#### probably best to add access functions to spacerItem //#### or even a QSpacerItem::flip() for (int i = 0; i < d->list.size(); ++i) { QBoxLayoutItem *box = d->list.at(i); if (box->magic) { QSpacerItem *sp = box->item->spacerItem(); if (sp) { if (sp->expandingDirections() == Qt::Orientations(0) /*No Direction*/) { //spacing or strut QSize s = sp->sizeHint(); sp->changeSize(s.height(), s.width(), horz(direction) ? QSizePolicy::Fixed:QSizePolicy::Minimum, horz(direction) ? QSizePolicy::Minimum:QSizePolicy::Fixed); } else { //stretch if (horz(direction)) sp->changeSize(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); else sp->changeSize(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding); } } } } } d->dir = direction; invalidate();}/*! \fn QBoxLayout::Direction QBoxLayout::direction() const Returns the direction of the box. addWidget() and addSpacing() work in this direction; the stretch stretches in this direction. \sa QBoxLayout::Direction addWidget() addSpacing()*/QBoxLayout::Direction QBoxLayout::direction() const{ Q_D(const QBoxLayout); return d->dir;}/*! \class QHBoxLayout \brief The QHBoxLayout class lines up widgets horizontally. \ingroup geomanagement \ingroup appearance \mainclass This class is used to construct horizontal box layout objects. See QBoxLayout for details. The simplest use of the class is like this: \quotefromfile snippets/layouts/layouts.cpp \skipto window = new QWidget \printline window \printline button1 \printuntil button5 \printline layout = new QHBoxLayout \printuntil window->setLayout(layout) \printline show First, we create the widgets we want in the layout. Then, we create the QHBoxLayout object and add the widgets into the layout. Finally, we call QWidget::setLayout() to install the QHBoxLayout object onto the widget. At that point, the widgets in the layout are reparented to have \c window as their parent. \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets \sa QVBoxLayout, QGridLayout, QStackedLayout, {Layout Classes}, {Basic Layouts Example}*//*! Constructs a new top-level horizontal box with parent \a parent.*/QHBoxLayout::QHBoxLayout(QWidget *parent) : QBoxLayout(LeftToRight, parent){}/*! Constructs a new horizontal box. You must add it to another layout.*/QHBoxLayout::QHBoxLayout() : QBoxLayout(LeftToRight){}#ifdef QT3_SUPPORT/*! Constructs a new top-level horizontal box called \a name, with parent \a parent. 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.*/QHBoxLayout::QHBoxLayout(QWidget *parent, int margin, int spacing, const char *name) : QBoxLayout(LeftToRight, parent){ setMargin(margin); setSpacing(spacing<0 ? margin : spacing); setObjectName(QString::fromAscii(name));}/*! Constructs a new horizontal box called name \a name and adds it to \a parentLayout. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1, this QHBoxLayout will inherit its parent's spacing().*/QHBoxLayout::QHBoxLayout(QLayout *parentLayout, int spacing, const char *name) : QBoxLayout(LeftToRight){ setSpacing(spacing); setObjectName(QString::fromAscii(name)); if (parentLayout) { setParent(parentLayout); parentLayout->addItem(this); }}/*! Constructs a new horizontal box called name \a name. You must add it to another layout. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1, this QHBoxLayout will inherit its parent's spacing().*/QHBoxLayout::QHBoxLayout(int spacing, const char *name) : QBoxLayout(LeftToRight){ setSpacing(spacing); setObjectName(QString::fromAscii(name));}#endif/*! Destroys this box layout. The layout's widgets aren't destroyed.*/QHBoxLayout::~QHBoxLayout(){}/*! \class QVBoxLayout \brief The QVBoxLayout class lines up widgets vertically. \ingroup geomanagement \ingroup appearance \mainclass This class is used to construct vertical box layout objects. See QBoxLayout for details. The simplest use of the class is like this: \quotefromfile snippets/layouts/layouts.cpp \skipto layout = new QHBoxLayout \skipto window = new QWidget \printline window \printline button1 \printuntil button5 \printline layout = new QVBoxLayout \printuntil window->setLayout(layout) \printline show First, we create the widgets we want in the layout. Then, we create the QVBoxLayout object and add the widgets into the layout. Finally, we call QWidget::setLayout() to install the QVBoxLayout object onto the widget. At that point, the widgets in the layout are reparented to have \c window as their parent. \image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets \sa QHBoxLayout, QGridLayout, QStackedLayout, {Layout Classes}, {Basic Layouts Example}*//*! Constructs a new top-level vertical box with parent \a parent.*/QVBoxLayout::QVBoxLayout(QWidget *parent) : QBoxLayout(TopToBottom, parent){}/*! Constructs a new vertical box. You must add it to another layout.*/QVBoxLayout::QVBoxLayout() : QBoxLayout(TopToBottom){}#ifdef QT3_SUPPORT/*! Constructs a new top-level vertical box called \a name, with parent \a parent. 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.*/QVBoxLayout::QVBoxLayout(QWidget *parent, int margin, int spacing, const char *name) : QBoxLayout(TopToBottom, parent){ setMargin(margin); setSpacing(spacing<0 ? margin : spacing); setObjectName(QString::fromAscii(name));}/*! Constructs a new vertical box called name \a name and adds it to \a parentLayout. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1, this QVBoxLayout will inherit its parent's spacing().*/QVBoxLayout::QVBoxLayout(QLayout *parentLayout, int spacing, const char *name) : QBoxLayout(TopToBottom){ setSpacing(spacing); setObjectName(QString::fromAscii(name)); if (parentLayout) { setParent(parentLayout); parentLayout->addItem(this); }}/*! Constructs a new vertical box called name \a name. You must add it to another layout. The \a spacing is the default number of pixels between neighboring children. If \a spacing is -1, this QVBoxLayout will inherit its parent's spacing().*/QVBoxLayout::QVBoxLayout(int spacing, const char *name) : QBoxLayout(TopToBottom){ setSpacing(spacing); setObjectName(QString::fromAscii(name));}#endif/*! Destroys this box layout. The layout's widgets aren't destroyed.*/QVBoxLayout::~QVBoxLayout(){}/*! \fn QWidget *QLayout::mainWidget() const Use parentWidget() instead.*//*! \fn void QLayout::remove(QWidget *widget) Use removeWidget(\a widget) instead.*//*! \fn void QLayout::add(QWidget *widget) Use addWidget(\a widget) instead.*//*! \fn QLayoutIterator QLayout::iterator() Use a QLayoutIterator() constructor instead.*//*! \fn int QLayout::defaultBorder() const Use spacing() instead.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -