You can assign a stretch factor to each item to control how much space it will get compared to the other items. By default, two identical widgets arranged in a linear layout will have the same size, but if the first widget has a stretch factor of 1 and the second widget has a stretch factor of 2, the first widget will get 1/3 of the available space, and the second will get 2/3.
QGraphicsLinearLayout calculates the distribution of sizes by adding up the stretch factors of all items, and then dividing the available space accordingly. The default stretch factor is 0 for all items; a factor of 0 means the item does not have any defined stretch factor; effectively this is the same as setting the stretch factor to 1. The stretch factor only applies to the available space in the lengthwise direction of the layout (following its orientation). If you want to control both the item's horizontal and vertical stretch, you can use QGraphicsGridLayout instead.
QGraphicsLinearLayout Compared to Other Layouts
QGraphicsLinearLayout is very similar to QVBoxLayout and QHBoxLayout, but in contrast to these classes, it is used to manage QGraphicsWidget and QGraphicsLayout instead of QWidget and QLayout.
See also QGraphicsGridLayout and QGraphicsWidget.
Property Documentation
This property holds the orientation of the linear layout.
This property toggles the layout orientation. Changing the layout orientation will automatically invalidate the layout.
Access functions:
- Qt::Orientation orientation () const
- void setOrientation ( Qt::Orientation orientation )
See also invalidate().
This property holds the layout's default spacing.
This property adjusts the layout's default spacing. The spacing applies to the vertical and horizontal distance between items.
Access functions:
- qreal spacing () const
- void setSpacing ( qreal spacing )
See also setItemSpacing(), setStretchFactor(), and QGraphicsGridLayout::setSpacing().
Member Function Documentation
QGraphicsLinearLayout::QGraphicsLinearLayout ( QGraphicsLayoutItem * parent = 0 )
Constructs a QGraphicsLinearLayout instance using Qt::Horizontal orientation. parent is passed to QGraphicsLayout's constructor.
QGraphicsLinearLayout::QGraphicsLinearLayout ( Qt::Orientation orientation, QGraphicsLayoutItem * parent = 0 )
Constructs a QGraphicsLinearLayout instance. You can pass the orientation for the layout, either horizontal or vertical, and parent is passed to QGraphicsLayout's constructor.
QGraphicsLinearLayout::~QGraphicsLinearLayout () [virtual]
Destroys the QGraphicsLinearLayout object.
void QGraphicsLinearLayout::addItem ( QGraphicsLayoutItem * item )
This convenience function is equivalent to calling insertItem(-1, item).
void QGraphicsLinearLayout::addStretch ( int stretch = 1 )
This convenience function is equivalent to calling insertStretch(-1, stretch).
Returns the alignment for item. The default alignment is Qt::AlignCenter.
The alignment decides how the item is positioned within its assigned space in the case where there's more space available in the layout than the widgets can occupy.
See also setAlignment().
void QGraphicsLinearLayout::insertItem ( int index, QGraphicsLayoutItem * item )
Inserts item into the layout at index, or before any item that is currently at index.
See also addItem(), itemAt(), insertStretch(), and setItemSpacing().
void QGraphicsLinearLayout::insertStretch ( int index, int stretch = 1 )
Inserts a stretch of stretch at index, or before any item that is currently at index.
See also addStretch(), setStretchFactor(), setItemSpacing(), and insertItem().
qreal QGraphicsLinearLayout::itemSpacing ( int index ) const
Returns the spacing after item at index.
See also setItemSpacing().
void QGraphicsLinearLayout::removeAt ( int index ) [virtual]
Removes the item at index without destroying it. Ownership of the item is transferred to the caller.
Reimplemented from QGraphicsLayout.
See also removeItem() and insertItem().
void QGraphicsLinearLayout::removeItem ( QGraphicsLayoutItem * item )
Removes item from the layout without destroying it. Ownership of item is transferred to the caller.
See also removeAt() and insertItem().
Sets the alignment of item to alignment. If item's alignment changes, the layout is automatically invalidated.
See also alignment() and invalidate().
void QGraphicsLinearLayout::setItemSpacing ( int index, qreal spacing )
Sets the spacing after item at index to spacing.
See also itemSpacing().
void QGraphicsLinearLayout::setStretchFactor ( QGraphicsLayoutItem * item, int stretch )
Sets the stretch factor for item to stretch. If an item's stretch factor changes, this function will invalidate the layout.
Setting stretch to 0 removes the stretch factor from the item, and is effectively equivalent to setting stretch to 1.
See also stretchFactor().
int QGraphicsLinearLayout::stretchFactor ( QGraphicsLayoutItem * item ) const
Returns the stretch factor for item. The default stretch factor is 0, meaning that the item has no assigned stretch factor.
See also setStretchFactor().