QLayoutItem Class

QLayoutItem 类提供了一个可由QLayout 操作的抽象项。更多

Header: #include <QLayoutItem>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承者

QLayout,QSpacerItem, 以及QWidgetItem

公共函数

QLayoutItem(Qt::Alignment alignment = Qt::Alignment())
virtual ~QLayoutItem()
Qt::Alignment alignment() const
virtual QSizePolicy::ControlTypes controlTypes() const
virtual Qt::Orientations expandingDirections() const = 0
virtual QRect geometry() const = 0
virtual bool hasHeightForWidth() const
virtual int heightForWidth(int) const
virtual void invalidate()
virtual bool isEmpty() const = 0
virtual QLayout *layout()
virtual QSize maximumSize() const = 0
virtual int minimumHeightForWidth(int w) const
virtual QSize minimumSize() const = 0
void setAlignment(Qt::Alignment alignment)
virtual void setGeometry(const QRect &r) = 0
virtual QSize sizeHint() const = 0
virtual QSpacerItem *spacerItem()
virtual QWidget *widget() const

详细说明

用于自定义布局。

提供的纯虚函数用于返回布局的相关信息,包括sizeHint(),minimumSize(),maximumSize() 和expandingDirections() 。

布局的几何形状可通过setGeometry() 和geometry() 设置和获取,对齐方式可通过setAlignment() 和alignment() 设置和获取。

isEmpty() 返回布局项是否为空。如果具体项目是QWidget ,则可以使用widget() 进行检索。同样,layout() 和spacerItem() 也是如此。

有些布局的宽度和高度相互依赖。这些可以用hasHeightForWidth(),heightForWidth() 和minimumHeightForWidth() 来表示。有关更多解释,请参阅《Qt 季刊》文章《用高度换宽度》。

另请参阅 QLayout

成员函数文档

[explicit] QLayoutItem::QLayoutItem(Qt::Alignment alignment = Qt::Alignment())

构造一个alignment 的布局项。并非所有子类都支持对齐方式。

[virtual noexcept] QLayoutItem::~QLayoutItem()

摧毁QLayoutItem.

Qt::Alignment QLayoutItem::alignment() const

返回此项目的对齐方式。

另请参见 setAlignment()。

[virtual] QSizePolicy::ControlTypes QLayoutItem::controlTypes() const

返回布局项的控制类型。对于QWidgetItem ,控制类型来自部件的尺寸策略;对于QLayoutItem ,控制类型来自布局的内容。

另请参阅 QSizePolicy::controlType().

[pure virtual] Qt::Orientations QLayoutItem::expandingDirections() const

返回此布局项是否可以使用比sizeHint() 更多的空间。Qt::VerticalQt::Horizontal 表示它只想在一个维度上增长,而Qt::Vertical |Qt::Horizontal 表示它想在两个维度上增长。

[pure virtual] QRect QLayoutItem::geometry() const

返回此布局项覆盖的矩形区域。

另请参阅 setGeometry()。

[virtual] bool QLayoutItem::hasHeightForWidth() const

如果此布局的首选高度取决于其宽度,则返回true ;否则返回false 。默认实现返回 false。

在支持高度换宽度的布局管理器中重新实现此函数。

另请参阅 heightForWidth() 和QWidget::heightForWidth()。

[virtual] int QLayoutItem::heightForWidth(int) const

在给定宽度的情况下,返回此布局项的首选高度,默认实现中不使用宽度。

默认实现返回-1,表示首选高度与项目的宽度无关。使用hasHeightForWidth() 函数通常比调用此函数并测试-1要快得多。

在支持高度换宽度的布局管理器中重新实现此函数。典型的实现方法如下:

int MyLayout::heightForWidth(int w) const
{
    if (cache_dirty || cached_width != w) {
        MyLayout *that = const_cast<MyLayout *>(this);
        int h = calculateHeightForWidth(w);
        that->cached_hfw = h;
        return h;
    }
    return cached_hfw;
}

强烈建议使用缓存,否则布局将耗费指数级的时间。

另请参见 hasHeightForWidth()。

[virtual] void QLayoutItem::invalidate()

使该布局项中的任何缓存信息失效。

[pure virtual] bool QLayoutItem::isEmpty() const

在子类中实现,用于返回此项目是否为空,即是否包含任何部件。

[virtual] QLayout *QLayoutItem::layout()

如果该项是QLayout ,则返回QLayout ;否则返回nullptr 。该函数提供了类型安全转换。

另请参阅 spacerItem() 和widget()。

[pure virtual] QSize QLayoutItem::maximumSize() const

在子类中实现,用于返回此项目的最大尺寸。

[virtual] int QLayoutItem::minimumHeightForWidth(int w) const

返回给定宽度下该 widget 所需的最小高度,w 。默认实现只是返回heightForWidth(w)。

[pure virtual] QSize QLayoutItem::minimumSize() const

在子类中实现,用于返回该项目的最小尺寸。

void QLayoutItem::setAlignment(Qt::Alignment alignment)

将此项目的对齐方式设置为alignment

注意:只有QLayoutItem 子类才支持项目对齐方式,因为它会产生视觉效果。除了为布局提供空白空间的QSpacerItem 之外,所有继承QLayoutItem 的 Qt 公共类都支持项目对齐方式。

另请参阅 alignment() 。

[pure virtual] void QLayoutItem::setGeometry(const QRect &r)

在子类中实现,将此项目的几何图形设置为r

另请参阅 geometry()。

[pure virtual] QSize QLayoutItem::sizeHint() const

在子类中实现,用于返回此项目的首选尺寸。

[virtual] QSpacerItem *QLayoutItem::spacerItem()

如果该项是QSpacerItem ,则返回QSpacerItem ;否则返回nullptr 。该函数提供了类型安全转换。

另请参阅 layout() 和widget()。

[virtual] QWidget *QLayoutItem::widget() const

如果该项目管理QWidget ,则返回该部件。否则,将返回nullptr

注意: 虽然函数layout() 和spacerItem() 进行了转换,但该函数返回另一个对象:QLayoutQSpacerItem 继承了QLayoutItem ,而QWidget 没有继承。

另请参阅 layout() 和spacerItem()。

© 2025 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.