本页内容

FlexboxLayout QML Type

FlexboxLayout QML 结构为快速项目提供了弹性布局。更多内容...

Import Statement: import QtQuick.Layouts
Since: Qt 6.10
Inherits:

Item

Status: Technology preview

此类型处于技术预览阶段,可能会有变动。

属性

关联属性

详细说明

FlexboxLayout 允许以类似于CSS 弹性盒布局的方式灵活地布局弹性项目。在内部,Qt FlexboxLayout 使用yoga 引擎来推导弹性项目的几何属性。yoga 库是CSS 弹性盒布局的一个子集。因此,FlexboxLayout 的功能范围仅限于yoga 库所支持的范围。

注意: FlexboxLayout 遵循 yoga 库 2.0 版本的功能规范。

可以通过现有的布局附加属性,为 FlexboxLayout 中的项目配置首选尺寸、最小尺寸和最大尺寸。例如,如果需要拉伸 FlexboxLayout 中的项目,可以设置布局附加属性Layout.fillWidth 或Layout.fillHeight 。

FlexboxLayout 中的项目支持以下附加属性:

点击此处进一步了解附加属性。

为了更高效地使用此类型,建议您了解Qt Quick Layouts 模块的一般工作原理。更多信息请参阅Qt Quick Layouts 概述。

使用示例

以下代码片段展示了使用 QML FlexboxLayout 以更灵活的方式排列矩形项的简明示例

FlexboxLayout {
    id: flexLayout
    anchors.fill: parent
    wrap: FlexboxLayout.Wrap
    direction: FlexboxLayout.Row
    justifyContent: FlexboxLayout.JustifySpaceAround
    Rectangle {
        color: 'teal'
        implicitWidth: 200
        implicitHeight: 200
    }
    Rectangle {
        color: 'plum'
        implicitWidth: 200
        implicitHeight: 200
    }
    Rectangle {
        color: 'olive'
        implicitWidth: 200
        implicitHeight: 200
    }
    Rectangle {
        color: 'beige'
        implicitWidth: 200
        implicitHeight: 200
    }
    Rectangle {
        color: 'darkseagreen'
        implicitWidth: 200
        implicitHeight: 200
    }
}

注意:此 API 目前处于技术预览阶段,在 Qt 的未来版本中可能会发生变更或被移除。

另请参阅 ColumnLayout 、GridLayout 、RowLayout 、StackView 以及Qt Quick Layouts 概述。

属性文档

alignContent : enumeration

此属性指定弹性行在弹性盒布局的横轴上的分布方式。

有效值:

Constant说明
FlexboxLayout.AlignStart弹性行对齐于弹性盒布局的起始位置。
FlexboxLayout.AlignCenter弹性行沿弹性盒布局的中心对齐。
FlexboxLayout.AlignEnd弹性行对齐到弹性盒布局的末尾。
FlexboxLayout.AlignStretch(默认) 弹性行会根据弹性盒布局的高度进行拉伸。
FlexboxLayout.AlignSpaceBetween空格在各行之间均匀分布,且弹性盒布局边缘不留空格。
FlexboxLayout.AlignSpaceAround行间距均匀分布,弹性盒布局边缘处为半行宽的间距。
FlexboxLayout.AlignSpaceEvenly行与弹性盒布局边缘之间的间距均匀分布。Qt 6.10 不支持此功能。

默认值为FlexboxLayout.AlignStart 。

alignItems : enumeration

该属性指定弹性盒布局中弹性行内项目的对齐方式,其沿交叉轴(该轴与主轴正交,由direction 属性定义)进行对齐。弹性盒布局中的项目可通过附加属性FlexboxLayout.alignSelf 覆盖此属性。

可能的取值:

常量描述
FlexboxLayout.AlignStart(默认)项目对齐于弹性盒布局横轴的起始点。
FlexboxLayout.AlignCenter项目沿弹性盒布局交叉轴的中心对齐。
FlexboxLayout.AlignEnd项目对齐到弹性盒布局横轴的末端。

注意: 可能值中提到的对齐方式 仅适用于 alignItems 属性

默认值为FlexboxLayout.AlignStart 。

columnGap : real

该属性用于指定沿内联轴向应用于FlexboxLayout 的间距。设置此属性将覆盖影响内联轴向的gap 值。

默认值为 `0`。

direction : enumeration

该属性控制弹性盒布局中项目排列的方向,并定义主轴。

可能的取值:

常量描述
FlexboxLayout.Row(默认)项目从左到右排列。
FlexboxLayout.RowReversed项目从右向左排列。
FlexboxLayout.Column项目从上到下排列。
FlexboxLayout.ColumnReversed项目从下到上排列。

默认值为FlexboxLayout.Row 。

gap : real

该属性指定了需要应用于FlexboxLayout 的、沿内联轴和块轴方向的空间量。

默认值为0 。

justifyContent : enumeration

此属性指定项目在弹性盒布局主轴上的分布方式。

有效值:

常量描述
FlexboxLayout.JustifyStart(默认)项目对齐于弹性盒布局的起始端。
FlexboxLayout.JustifyCenter项目沿弹性盒布局的中心对齐。
FlexboxLayout.JustifyEnd项目对齐到弹性盒布局的末端。
FlexboxLayout.JustifySpaceBetween间距在项目之间均匀分布,且弹性盒布局边缘不留间距。
FlexboxLayout.JustifySpaceAround项目之间的间距均匀分布,弹性盒布局边缘的间距为项目间距的一半。
FlexboxLayout.JustiftSpaceEvenly间距在项目与弹性盒布局边缘之间均匀分布。

默认值为FlexboxLayout.JustifyStart 。

rowGap : real

该属性用于指定沿块轴方向需应用于FlexboxLayout 的间距。设置此属性将覆盖影响块轴的gap 值。

默认值为0 。

wrap : enumeration

该属性用于指定弹性盒布局中的项目是否进行换行,当子元素超出弹性盒布局的大小时,会触发换行。如果项目进行换行,则会根据所述的溢出情况被分置在多行中。每行在横轴方向上占用的空间为该项目的最大尺寸。

可能的取值:

常量描述
FlexboxLayout.Wrap项目在弹性盒布局内折行排列为多行。
FlexboxLayout.NoWrap(默认) 项目不换行,在弹性盒布局中以单行排列。
FlexboxLayout.WrapReverse项目在弹性盒布局中按相反方向换行。

默认值为FlexboxLayout.NoWrap 。

相关属性文档

FlexboxLayout.alignSelf : enumeration [attached]

此附加属性允许在弹性盒布局中沿交叉轴对齐该项目,并会覆盖父级弹性盒布局属性alignItems 。

默认情况下,子项会继承父项的对齐方式,并可通过设置值FlexboxLayout.AlignStart 、FlexboxLayout.AlignCenter 和FlexboxLayout.AlignEnd 来覆盖父项的弹性盒布局属性alignItems 。

默认值为FlexboxLayout.AlignAuto 。

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