QGraphicsGridLayout Class
QGraphicsGridLayout 类提供了一种网格布局,用于在 Graphics View 中管理小部件。更多内容...
| 头文件: | #include <QGraphicsGridLayout> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) 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, Qt::Alignment alignment = Qt::Alignment()) |
| void | addItem(QGraphicsLayoutItem *item, int row, int column, int rowSpan, int columnSpan, 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 |
详细说明
使用 QGraphicsGridLayout 最常见的方式是在堆上构造一个对象,向构造函数传递一个父控件,然后通过调用addItem() 来添加控件和布局。随着您添加项目,QGraphicsGridLayout 会自动计算网格的尺寸。
QGraphicsScene scene;
QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);
QGraphicsWidget *form = new QGraphicsWidget;
scene.addItem(form);
QGraphicsGridLayout *layout = new QGraphicsGridLayout(form);
layout->addItem(textEdit, 0, 0);
layout->addItem(pushButton, 0, 1);或者,如果您未将父小部件传递给布局的构造函数,则需要调用 `QGraphicsWidget::setLayout()` 将此布局设置为该小部件的顶级布局,此时该小部件将拥有该布局的所有权。
布局将拥有这些项的所有权。在某些情况下,当布局项也继承自QGraphicsItem (例如QGraphicsWidget )时,由于该布局项属于两个所有权层次结构,因此会出现所有权歧义。 请参阅QGraphicsLayoutItem::setOwnedByLayout() 的文档了解如何处理此情况。您可以通过调用count() 和itemAt() 访问布局中的每个项。调用removeAt() 将从布局中移除一个项,但不会销毁该项。
QGraphicsGridLayout 中的尺寸提示和尺寸策略
QGraphicsGridLayout 会遵循每个项的大小提示和大小策略;当网格中的单元格空间大于项所能占用的空间时,每个项将根据布局对该项设定的对齐方式进行排列。 您可以通过调用setAlignment() 为每个项目设置对齐方式,并通过调用alignment() 检查任何项目的对齐方式。您还可以分别通过调用setRowAlignment() 和setColumnAlignment() 为整个行或列设置对齐方式。默认情况下,项目对齐于左上角。
另请参阅 QGraphicsLinearLayout 和QGraphicsWidget 。
成员函数文档
QGraphicsGridLayout::QGraphicsGridLayout(QGraphicsLayoutItem *parent = nullptr)
创建一个 QGraphicsGridLayout 实例。将 `parent ` 传递给 `QGraphicsLayout` 的构造函数。
[virtual noexcept] QGraphicsGridLayout::~QGraphicsGridLayout()
销毁QGraphicsGridLayout 对象。
void QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column, Qt::Alignment alignment = Qt::Alignment())
将item 添加到row 和column 上的网格中。您可以为item 指定可选的alignment 。
void QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
在row 和column 的网格中添加item 。您可以指定rowSpan 和columnSpan ,以及可选的alignment 。
Qt::Alignment QGraphicsGridLayout::alignment(QGraphicsLayoutItem *item) const
返回item 的对齐方式。
另请参阅 setAlignment()。
Qt::Alignment QGraphicsGridLayout::columnAlignment(int column) const
返回column 的对齐方式。
另请参阅 setColumnAlignment()。
int QGraphicsGridLayout::columnCount() const
返回网格布局中的列数。该数值始终比被布局项占用的最后一列的索引大1(空列会被计入,但末尾的空列除外)。
qreal QGraphicsGridLayout::columnMaximumWidth(int column) const
返回column 的最大宽度。
另请参阅 setColumnMaximumWidth()。
qreal QGraphicsGridLayout::columnMinimumWidth(int column) const
返回column 的最小宽度。
另请参阅 setColumnMinimumWidth()。
qreal QGraphicsGridLayout::columnPreferredWidth(int column) const
返回column 的首选宽度。
另请参阅 setColumnPreferredWidth()。
qreal QGraphicsGridLayout::columnSpacing(int column) const
返回column 的列间距。
另请参阅 setColumnSpacing()。
int QGraphicsGridLayout::columnStretchFactor(int column) const
返回column 的拉伸因子。
另请参阅 setColumnStretchFactor()。
[override virtual] int QGraphicsGridLayout::count() const
重新实现了:QGraphicsLayout::count() const。
返回此网格布局中布局项的数量。
qreal QGraphicsGridLayout::horizontalSpacing() const
返回网格布局的默认水平间距。
另请参阅 ` setHorizontalSpacing()`。
[override virtual] void QGraphicsGridLayout::invalidate()
重写了:QGraphicsLayout::invalidate()。
[override virtual] QGraphicsLayoutItem *QGraphicsGridLayout::itemAt(int index) const
重写了:QGraphicsLayout::itemAt(int i) const。
返回位于index 处的布局项;如果该索引处没有布局项,则返回nullptr 。
QGraphicsLayoutItem *QGraphicsGridLayout::itemAt(int row, int column) const
返回指向位于 (row,column) 处的布局项的指针。
[override virtual] void QGraphicsGridLayout::removeAt(int index)
重写了:QGraphicsLayout::removeAt(int index)。
从index 位置移除布局项,但不销毁该项。该项的所有权转移给调用方。
另请参阅 addItem()。
void QGraphicsGridLayout::removeItem(QGraphicsLayoutItem *item)
移除布局项item ,但不销毁该项。该项的所有权将转移给调用者。
另请参阅 addItem()。
Qt::Alignment QGraphicsGridLayout::rowAlignment(int row) const
返回row 的对齐方式。
另请参阅 setRowAlignment()。
int QGraphicsGridLayout::rowCount() const
返回网格布局中的行数。该数值始终比布局项所占用的最后一行索引多1(空行也会被计入,但末尾的空行除外)。
qreal QGraphicsGridLayout::rowMaximumHeight(int row) const
返回行row 的最大高度。
另请参阅 setRowMaximumHeight()。
qreal QGraphicsGridLayout::rowMinimumHeight(int row) const
返回行row 的最小高度。
另请参阅 setRowMinimumHeight()。
qreal QGraphicsGridLayout::rowPreferredHeight(int row) const
返回行row 的首选高度。
另请参阅 setRowPreferredHeight()。
qreal QGraphicsGridLayout::rowSpacing(int row) const
返回row 的行间距。
另请参阅 setRowSpacing()。
int QGraphicsGridLayout::rowStretchFactor(int row) const
返回row 的拉伸因子。
另请参阅 setRowStretchFactor()。
void QGraphicsGridLayout::setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
将item 的对齐方式设置为alignment 。
另请参阅 alignment()。
void QGraphicsGridLayout::setColumnAlignment(int column, Qt::Alignment alignment)
将column 的对齐方式设置为alignment 。
另请参阅 columnAlignment()。
void QGraphicsGridLayout::setColumnFixedWidth(int column, qreal width)
将column 的固定宽度设置为width 。
void QGraphicsGridLayout::setColumnMaximumWidth(int column, qreal width)
将column 的最大宽度设置为width 。
另请参阅 columnMaximumWidth()。
void QGraphicsGridLayout::setColumnMinimumWidth(int column, qreal width)
将column 的最小宽度设置为width 。
另请参阅 columnMinimumWidth()。
void QGraphicsGridLayout::setColumnPreferredWidth(int column, qreal width)
将column 的首选宽度设置为width 。
另请参阅 columnPreferredWidth()。
void QGraphicsGridLayout::setColumnSpacing(int column, qreal spacing)
将column 的间距设置为spacing 。
另请参阅 columnSpacing()。
void QGraphicsGridLayout::setColumnStretchFactor(int column, int stretch)
将column 的拉伸因子设置为stretch 。
另请参阅 columnStretchFactor()。
[override virtual] void QGraphicsGridLayout::setGeometry(const QRectF &rect)
重写了:QGraphicsLayoutItem::setGeometry(const QRectF &rect)。
将网格布局的边界几何设置为 `rect`。
void QGraphicsGridLayout::setHorizontalSpacing(qreal spacing)
将网格布局的默认水平间距设置为spacing 。
另请参阅 horizontalSpacing()。
void QGraphicsGridLayout::setRowAlignment(int row, Qt::Alignment alignment)
将row 的对齐方式设置为alignment 。
另请参阅 rowAlignment()。
void QGraphicsGridLayout::setRowFixedHeight(int row, qreal height)
将行row 的固定高度设置为height 。
void QGraphicsGridLayout::setRowMaximumHeight(int row, qreal height)
将行row 的最大高度设置为height 。
另请参阅 rowMaximumHeight()。
void QGraphicsGridLayout::setRowMinimumHeight(int row, qreal height)
将行(row )的最小高度设置为height 。
另请参阅 rowMinimumHeight()。
void QGraphicsGridLayout::setRowPreferredHeight(int row, qreal height)
将行row 的首选高度设置为height 。
另请参阅 rowPreferredHeight()。
void QGraphicsGridLayout::setRowSpacing(int row, qreal spacing)
将row 的间距设置为spacing 。
另请参阅 rowSpacing()。
void QGraphicsGridLayout::setRowStretchFactor(int row, int stretch)
将row 的拉伸因子设置为stretch 。
另请参阅 rowStretchFactor()。
void QGraphicsGridLayout::setSpacing(qreal spacing)
将网格布局的默认间距(包括垂直和水平方向)设置为spacing 。
另请参阅 rowSpacing() 和columnSpacing()。
void QGraphicsGridLayout::setVerticalSpacing(qreal spacing)
将网格布局的默认垂直间距设置为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
返回网格布局的默认垂直间距。
另请参阅 ` setVerticalSpacing()`。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.