QGraphicsGridLayout 类

QGraphicsGridLayout 类提供在图形视图管理 Widget 的栅格布局。 更多...

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

公共函数

  QGraphicsGridLayout (QGraphicsLayoutItem * parent = nullptr)
virtual ~QGraphicsGridLayout ()
void addItem (QGraphicsLayoutItem * item , int row , int column , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())
void addItem (QGraphicsLayoutItem * item , int row , int column , Qt::Alignment alignment = Qt::Alignment())
Qt::Alignment alignment (QGraphicsLayoutItem * item ) const
Qt::Alignment columnAlignment (int column ) const
int columnCount () const
qreal columnMaximumWidth (int column ) const
qreal columnMinimumWidth (int column ) const
qreal columnPreferredWidth (int column ) const
qreal columnSpacing (int column ) const
int columnStretchFactor (int column ) const
qreal horizontalSpacing () const
QGraphicsLayoutItem * itemAt (int row , int column ) const
void removeItem (QGraphicsLayoutItem * item )
Qt::Alignment rowAlignment (int row ) const
int rowCount () const
qreal rowMaximumHeight (int row ) const
qreal rowMinimumHeight (int row ) const
qreal rowPreferredHeight (int row ) const
qreal rowSpacing (int row ) const
int rowStretchFactor (int row ) const
void setAlignment (QGraphicsLayoutItem * item , Qt::Alignment alignment )
void setColumnAlignment (int column , Qt::Alignment alignment )
void setColumnFixedWidth (int column , qreal width )
void setColumnMaximumWidth (int column , qreal width )
void setColumnMinimumWidth (int column , qreal width )
void setColumnPreferredWidth (int column , qreal width )
void setColumnSpacing (int column , qreal spacing )
void setColumnStretchFactor (int column , int stretch )
void setHorizontalSpacing (qreal spacing )
void setRowAlignment (int row , Qt::Alignment alignment )
void setRowFixedHeight (int row , qreal height )
void setRowMaximumHeight (int row , qreal height )
void setRowMinimumHeight (int row , qreal height )
void setRowPreferredHeight (int row , qreal height )
void setRowSpacing (int row , qreal spacing )
void setRowStretchFactor (int row , int stretch )
void setSpacing (qreal spacing )
void setVerticalSpacing (qreal spacing )
qreal verticalSpacing () 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 most common way to use QGraphicsGridLayout is to construct an object on the heap with no parent, add widgets and layouts by calling addItem (), and finally assign the layout to a widget by calling QGraphicsWidget::setLayout (). QGraphicsGridLayout automatically computes the dimensions of the grid as you add items.

QGraphicsScene scene;
QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);
QGraphicsGridLayout *layout = new QGraphicsGridLayout;
layout->addItem(textEdit, 0, 0);
layout->addItem(pushButton, 0, 1);
QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
scene.addItem(form);
					

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 () will remove an item from the layout, without destroying it.

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

QGraphicsGridLayout respects each item's size hints and size policies, and when a cell in the grid has 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 (). You can also set the alignment for an entire row or column by calling setRowAlignment () 和 setColumnAlignment () respectively. By default, items are aligned to the top left.

另请参阅 QGraphicsLinearLayout and QGraphicsWidget .

成员函数文档编制

QGraphicsGridLayout:: QGraphicsGridLayout ( QGraphicsLayoutItem * parent = nullptr)

Constructs a QGraphicsGridLayout instance. parent 会被传递给 QGraphicsLayout 的构造函数。

[虚拟] QGraphicsGridLayout:: ~QGraphicsGridLayout ()

销毁 QGraphicsGridLayout 对象。

void QGraphicsGridLayout:: addItem ( QGraphicsLayoutItem * item , int row , int column , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())

添加 item to the grid on row and column . You can specify a rowSpan and columnSpan and an optional alignment .

void QGraphicsGridLayout:: addItem ( QGraphicsLayoutItem * item , int row , int column , Qt::Alignment alignment = Qt::Alignment())

添加 item to the grid on row and column . You can specify an optional alignment for item .

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

返回对齐方式为 item .

另请参阅 setAlignment ().

Qt::Alignment QGraphicsGridLayout:: columnAlignment ( int column ) const

返回对齐方式为 column .

另请参阅 setColumnAlignment ().

int QGraphicsGridLayout:: columnCount () const

Returns the number of columns in the grid layout. This is always one more than the index of the last column that is occupied by a layout item (empty columns are counted except for those at the end).

qreal QGraphicsGridLayout:: columnMaximumWidth ( int column ) const

返回最大宽度为 column .

另请参阅 setColumnMaximumWidth ().

qreal QGraphicsGridLayout:: columnMinimumWidth ( int column ) const

Returns the minimum width for column .

另请参阅 setColumnMinimumWidth ().

qreal QGraphicsGridLayout:: columnPreferredWidth ( int column ) const

Returns the preferred width for column .

另请参阅 setColumnPreferredWidth ().

qreal QGraphicsGridLayout:: columnSpacing ( int column ) const

Returns the column spacing for column .

另请参阅 setColumnSpacing ().

int QGraphicsGridLayout:: columnStretchFactor ( int column ) const

Returns the stretch factor for column .

另请参阅 setColumnStretchFactor ().

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

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

Returns the number of layout items in this grid layout.

qreal QGraphicsGridLayout:: horizontalSpacing () const

Returns the default horizontal spacing for the grid layout.

另请参阅 setHorizontalSpacing ().

[override virtual] void QGraphicsGridLayout:: invalidate ()

重实现: QGraphicsLayout::invalidate ().

QGraphicsLayoutItem *QGraphicsGridLayout:: itemAt ( int row , int column ) const

Returns a pointer to the layout item at ( row , column ).

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

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

Returns the layout item at index ,或 nullptr if there is no layout item at this index.

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

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

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

另请参阅 addItem ().

void QGraphicsGridLayout:: removeItem ( QGraphicsLayoutItem * item )

移除布局项 item without destroying it. Ownership of the item is transferred to the caller.

另请参阅 addItem ().

Qt::Alignment QGraphicsGridLayout:: rowAlignment ( int row ) const

Returns the alignment of row .

另请参阅 setRowAlignment ().

int QGraphicsGridLayout:: rowCount () const

Returns the number of rows in the grid layout. This is always one more than the index of the last row that is occupied by a layout item (empty rows are counted except for those at the end).

qreal QGraphicsGridLayout:: rowMaximumHeight ( int row ) const

Returns the maximum height for row, row .

另请参阅 setRowMaximumHeight ().

qreal QGraphicsGridLayout:: rowMinimumHeight ( int row ) const

Returns the minimum height for row, row .

另请参阅 setRowMinimumHeight ().

qreal QGraphicsGridLayout:: rowPreferredHeight ( int row ) const

Returns the preferred height for row, row .

另请参阅 setRowPreferredHeight ().

qreal QGraphicsGridLayout:: rowSpacing ( int row ) const

Returns the row spacing for row .

另请参阅 setRowSpacing ().

int QGraphicsGridLayout:: rowStretchFactor ( int row ) const

Returns the stretch factor for row .

另请参阅 setRowStretchFactor ().

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

Sets the alignment for item to alignment .

另请参阅 alignment ().

void QGraphicsGridLayout:: setColumnAlignment ( int column , Qt::Alignment alignment )

Sets the alignment for column to alignment .

另请参阅 columnAlignment ().

void QGraphicsGridLayout:: setColumnFixedWidth ( int column , qreal width )

Sets the fixed width of column to width .

void QGraphicsGridLayout:: setColumnMaximumWidth ( int column , qreal width )

Sets the maximum width of column to width .

另请参阅 columnMaximumWidth ().

void QGraphicsGridLayout:: setColumnMinimumWidth ( int column , qreal width )

Sets the minimum width for column to width .

另请参阅 columnMinimumWidth ().

void QGraphicsGridLayout:: setColumnPreferredWidth ( int column , qreal width )

Sets the preferred width for column to width .

另请参阅 columnPreferredWidth ().

void QGraphicsGridLayout:: setColumnSpacing ( int column , qreal spacing )

Sets the spacing for column to spacing .

另请参阅 columnSpacing ().

void QGraphicsGridLayout:: setColumnStretchFactor ( int column , int stretch )

设置拉伸因子为 column to stretch .

另请参阅 columnStretchFactor ().

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

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

Sets the bounding geometry of the grid layout to rect .

void QGraphicsGridLayout:: setHorizontalSpacing ( qreal spacing )

Sets the default horizontal spacing for the grid layout to spacing .

另请参阅 horizontalSpacing ().

void QGraphicsGridLayout:: setRowAlignment ( int row , Qt::Alignment alignment )

Sets the alignment of row to alignment .

另请参阅 rowAlignment ().

void QGraphicsGridLayout:: setRowFixedHeight ( int row , qreal height )

Sets the fixed height for row, row ,到 height .

void QGraphicsGridLayout:: setRowMaximumHeight ( int row , qreal height )

Sets the maximum height for row, row ,到 height .

另请参阅 rowMaximumHeight ().

void QGraphicsGridLayout:: setRowMinimumHeight ( int row , qreal height )

Sets the minimum height for row, row ,到 height .

另请参阅 rowMinimumHeight ().

void QGraphicsGridLayout:: setRowPreferredHeight ( int row , qreal height )

Sets the preferred height for row, row ,到 height .

另请参阅 rowPreferredHeight ().

void QGraphicsGridLayout:: setRowSpacing ( int row , qreal spacing )

Sets the spacing for row to spacing .

另请参阅 rowSpacing ().

void QGraphicsGridLayout:: setRowStretchFactor ( int row , int stretch )

设置拉伸因子为 row to stretch .

另请参阅 rowStretchFactor ().

void QGraphicsGridLayout:: setSpacing ( qreal spacing )

Sets the grid layout's default spacing, both vertical and horizontal, to spacing .

另请参阅 rowSpacing () 和 columnSpacing ().

void QGraphicsGridLayout:: setVerticalSpacing ( qreal spacing )

Sets the default vertical spacing for the grid layout to spacing .

另请参阅 verticalSpacing ().

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

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

qreal QGraphicsGridLayout:: verticalSpacing () const

Returns the default vertical spacing for the grid layout.

另请参阅 setVerticalSpacing ().