QGraphicsLinearLayout 类

QGraphicsLinearLayout 类提供用于在图形视图中管理 Widget 的水平或垂直布局。 更多...

头: #include <QGraphicsLinearLayout>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QGraphicsLayout

公共函数

QGraphicsLinearLayout (QGraphicsLayoutItem * parent = nullptr)
QGraphicsLinearLayout (Qt::Orientation orientation , QGraphicsLayoutItem * parent = nullptr)
virtual ~QGraphicsLinearLayout ()
void addItem (QGraphicsLayoutItem * item )
void addStretch (int stretch = 1)
Qt::Alignment alignment (QGraphicsLayoutItem * item ) const
void insertItem (int index , QGraphicsLayoutItem * item )
void insertStretch (int index , int stretch = 1)
qreal itemSpacing (int index ) const
Qt::Orientation orientation () const
void removeItem (QGraphicsLayoutItem * item )
void setAlignment (QGraphicsLayoutItem * item , Qt::Alignment alignment )
void setItemSpacing (int index , qreal spacing )
void setOrientation (Qt::Orientation orientation )
void setSpacing (qreal spacing )
void setStretchFactor (QGraphicsLayoutItem * item , int stretch )
qreal spacing () const
int stretchFactor (QGraphicsLayoutItem * item ) const

重实现公共函数

virtual int count () const override
virtual void invalidate () override
virtual QGraphicsLayoutItem * itemAt (int index ) const override
virtual void removeAt (int index ) override
virtual void setGeometry (const QRectF & rect ) override
virtual QSizeF sizeHint (Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const override

详细描述

The default orientation for a linear layout is Qt::Horizontal . You can choose a vertical orientation either by calling setOrientation (), or by passing Qt::Vertical to QGraphicsLinearLayout's constructor.

The most common way to use QGraphicsLinearLayout is to construct an object on the heap, passing a parent widget to the constructor, then add widgets and layouts by calling addItem ().

QGraphicsScene scene;
QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);
QGraphicsWidget *form = new QGraphicsWidget;
scene.addItem(form);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(form);
layout->addItem(textEdit);
layout->addItem(pushButton);
					

Alternatively, if you do not pass a parent widget to the layout's constructor, you will need to call QGraphicsWidget::setLayout () to set this layout as the top-level layout for that widget, the widget will take ownership of the layout.

You can add widgets, layouts, stretches ( addStretch (), insertStretch () 或 setStretchFactor ()), and spacings ( setItemSpacing ()) to a linear layout. The layout takes ownership of the items. In some cases when the layout item also inherits from QGraphicsItem (譬如 QGraphicsWidget ) there will be a ambiguity in ownership because the layout item belongs to two ownership hierarchies. See the documentation of QGraphicsLayoutItem::setOwnedByLayout () how to handle this. You can access each item in the layout by calling count () 和 itemAt ()。调用 removeAt () 或 removeItem () will remove an item from the layout, without destroying it.

在 QGraphicsLinearLayout 中的大小提示和大小策略

QGraphicsLinearLayout respects each item's size hints and size policies, and when the layout contains more space than the items can fill, each item is arranged according to the layout's alignment for that item. You can set an alignment for each item by calling setAlignment (), and check the alignment for any item by calling alignment (). By default, items are aligned to the top left.

在 QGraphicsLinearLayout 中的间距

Between the items, the layout distributes some space. The actual amount of space depends on the managed widget's current style, but the common spacing is 4. You can also set your own spacing by calling setSpacing (), and get the current spacing value by calling spacing (). If you want to configure individual spacing for your items, you can call setItemSpacing ().

在 QGraphicsLinearLayout 中的拉伸因子

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 代替。

QGraphicsLinearLayout 相比其它布局

QGraphicsLinearLayout is very similar to QVBoxLayout and QHBoxLayout , but in contrast to these classes, it is used to manage QGraphicsWidget and QGraphicsLayout 而不是 QWidget and QLayout .

另请参阅 QGraphicsGridLayout and QGraphicsWidget .

成员函数文档编制

QGraphicsLinearLayout:: QGraphicsLinearLayout ( QGraphicsLayoutItem * parent = nullptr)

Constructs a QGraphicsLinearLayout instance using Qt::Horizontal orientation. parent 会被传递给 QGraphicsLayout 的构造函数。

QGraphicsLinearLayout:: QGraphicsLinearLayout ( Qt::Orientation orientation , QGraphicsLayoutItem * parent = nullptr)

Constructs a QGraphicsLinearLayout instance. You can pass the orientation for the layout, either horizontal or vertical, and parent 会被传递给 QGraphicsLayout 的构造函数。

[virtual noexcept] QGraphicsLinearLayout:: ~QGraphicsLinearLayout ()

销毁 QGraphicsLinearLayout 对象。

void QGraphicsLinearLayout:: addItem ( QGraphicsLayoutItem * item )

此方便函数相当于调用 insertItem (-1, item ).

void QGraphicsLinearLayout:: addStretch ( int stretch = 1)

此方便函数相当于调用 insertStretch (-1, stretch ).

Qt::Alignment QGraphicsLinearLayout:: alignment ( QGraphicsLayoutItem * item ) const

返回对齐方式为 item . The default alignment is Qt::AlignTop | Qt::AlignLeft .

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.

另请参阅 setAlignment ().

[override virtual] int QGraphicsLinearLayout:: count () const

重实现: QGraphicsLayout::count() const .

void QGraphicsLinearLayout:: insertItem ( int index , QGraphicsLayoutItem * item )

插入 item into the layout at index , or before any item that is currently at index .

另请参阅 addItem (), itemAt (), insertStretch (),和 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 .

另请参阅 addStretch (), setStretchFactor (), setItemSpacing (),和 insertItem ().

[override virtual] void QGraphicsLinearLayout:: invalidate ()

重实现: QGraphicsLayout::invalidate ().

[override virtual] QGraphicsLayoutItem *QGraphicsLinearLayout:: itemAt ( int index ) const

重实现: QGraphicsLayout::itemAt(int i) const .

When iterating from 0 and up, it will return the items in the visual arranged order.

qreal QGraphicsLinearLayout:: itemSpacing ( int index ) const

Returns the spacing after item at index .

另请参阅 setItemSpacing ().

Qt::Orientation QGraphicsLinearLayout:: orientation () const

Returns the layout orientation.

另请参阅 setOrientation ().

[override virtual] void QGraphicsLinearLayout:: removeAt ( int index )

重实现: QGraphicsLayout::removeAt (int index).

移除项在 index without destroying it. Ownership of the item is transferred to the caller.

另请参阅 removeItem () 和 insertItem ().

void QGraphicsLinearLayout:: removeItem ( QGraphicsLayoutItem * item )

移除 item from the layout without destroying it. Ownership of item is transferred to the caller.

另请参阅 removeAt () 和 insertItem ().

void QGraphicsLinearLayout:: setAlignment ( QGraphicsLayoutItem * item , Qt::Alignment alignment )

Sets the alignment of item to alignment 。若 item 's alignment changes, the layout is automatically invalidated.

另请参阅 alignment () 和 invalidate ().

[override virtual] void QGraphicsLinearLayout:: setGeometry (const QRectF & rect )

重实现: QGraphicsLayoutItem::setGeometry (const QRectF &rect).

void QGraphicsLinearLayout:: setItemSpacing ( int index , qreal spacing )

Sets the spacing after item at index to spacing .

另请参阅 itemSpacing ().

void QGraphicsLinearLayout:: setOrientation ( Qt::Orientation orientation )

Change the layout orientation to orientation . Changing the layout orientation will automatically invalidate the layout.

另请参阅 orientation ().

void QGraphicsLinearLayout:: setSpacing ( qreal spacing )

Sets the layout's spacing to spacing . Spacing refers to the vertical and horizontal distances between items.

另请参阅 spacing (), setItemSpacing (), setStretchFactor (),和 QGraphicsGridLayout::setSpacing ().

void QGraphicsLinearLayout:: setStretchFactor ( QGraphicsLayoutItem * item , int stretch )

设置拉伸因子为 item to stretch . If an item's stretch factor changes, this function will invalidate the layout.

设置 stretch to 0 removes the stretch factor from the item, and is effectively equivalent to setting stretch to 1.

另请参阅 stretchFactor ().

[override virtual] QSizeF QGraphicsLinearLayout:: sizeHint ( Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const

重实现: QGraphicsLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const .

qreal QGraphicsLinearLayout:: spacing () const

Returns the layout's spacing. Spacing refers to the vertical and horizontal distances between items.

另请参阅 setSpacing ().

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.

另请参阅 setStretchFactor ().