布局
FlexLayout 弹性布局
受 CSS Flexbox 启发的 QLayout 布局管理器,支持主轴/交叉轴对齐、自动换行及按项弹性伸缩。
示例
属性预览加载失败。请再次点击属性预览重试。

用法
换行与弹性拉伸
为父控件安装 FlexLayout,并用 ItemOptions 配置 flexGrow、order 与对齐方式。
#include <FluentQt/Layout.h>
using namespace fluent::layout;
#pragma once
#include <FluentQt/FluentQt.h>
#include <QBoxLayout>
#include <algorithm>
namespace fluent::gallery {
class FlexLayoutSample final : public QWidget {
public:
explicit FlexLayoutSample(QWidget* parent = nullptr) : QWidget(parent)
{
auto* vbox = new QVBoxLayout(this);
vbox->setContentsMargins(0, 0, 0, 0);
vbox->setSpacing(10);
auto* controls = new QWidget(this);
auto* ctrlRow = new QHBoxLayout(controls);
ctrlRow->setContentsMargins(0, 0, 0, 0);
ctrlRow->setSpacing(10);
auto* justify = new basicinput::ComboBox(controls);
justify->setAccessibleName(QStringLiteral("Justify content"));
justify->addItems({QStringLiteral("FlexStart"), QStringLiteral("Center"), QStringLiteral("FlexEnd"),
QStringLiteral("SpaceBetween"), QStringLiteral("SpaceAround")});
auto* wrapCheck = new basicinput::CheckBox(QStringLiteral("Wrap lines"), controls);
wrapCheck->setChecked(true);
auto* growCheck = new basicinput::CheckBox(QStringLiteral("Grow primary (flexGrow)"), controls);
growCheck->setChecked(false);
ctrlRow->addWidget(justify);
ctrlRow->addWidget(wrapCheck);
ctrlRow->addWidget(growCheck);
ctrlRow->addStretch();
vbox->addWidget(controls);
auto* widthBar = new QWidget(this);
auto* widthRow = new QHBoxLayout(widthBar);
widthRow->setContentsMargins(0, 0, 0, 0);
widthRow->setSpacing(10);
auto* widthLabel = new textfields::Label(QStringLiteral("Container width: 340 px"), widthBar);
auto* widthSlider = new basicinput::Slider(Qt::Horizontal, widthBar);
widthSlider->setAccessibleName(QStringLiteral("Container width"));
widthSlider->setRange(220, 560);
widthSlider->setValue(340);
widthSlider->setFixedWidth(220);
widthRow->addWidget(widthLabel);
widthRow->addWidget(widthSlider);
widthRow->addStretch();
vbox->addWidget(widthBar);
auto* surface = new layout::Card(this);
auto* flex = new layout::FlexLayout(surface);
flex->setContentsMargins(12, 12, 12, 12);
flex->setGap(8);
flex->setWrap(layout::FlexLayout::Wrap::Wrap);
auto* primary = new basicinput::Button(QStringLiteral("Primary"), surface);
primary->setFluentStyle(basicinput::Button::Accent);
flex->addWidget(primary);
flex->addWidget(new basicinput::Button(QStringLiteral("Filters"), surface));
flex->addWidget(new basicinput::Button(QStringLiteral("Metrics"), surface));
flex->addWidget(new basicinput::Button(QStringLiteral("Alerts"), surface));
flex->addWidget(new basicinput::Button(QStringLiteral("Export"), surface));
flex->addWidget(new basicinput::Button(QStringLiteral("Settings"), surface));
vbox->addWidget(surface);
const auto applyWidth = [surface, flex](int w) {
surface->setFixedWidth(w);
surface->setFixedHeight(std::max(56, flex->heightForWidth(w)));
};
applyWidth(340);
QObject::connect(justify, qOverload<int>(&basicinput::ComboBox::currentIndexChanged), flex, [flex](int idx) {
const layout::FlexLayout::JustifyContent modes[] = {layout::FlexLayout::JustifyContent::FlexStart,
layout::FlexLayout::JustifyContent::Center, layout::FlexLayout::JustifyContent::FlexEnd,
layout::FlexLayout::JustifyContent::SpaceBetween, layout::FlexLayout::JustifyContent::SpaceAround};
if (idx >= 0 && idx < 5)
flex->setJustifyContent(modes[idx]);
});
QObject::connect(wrapCheck, &basicinput::CheckBox::toggled, flex, [flex, widthSlider, applyWidth](bool on) {
flex->setWrap(on ? layout::FlexLayout::Wrap::Wrap : layout::FlexLayout::Wrap::NoWrap);
applyWidth(widthSlider->value());
});
QObject::connect(growCheck, &basicinput::CheckBox::toggled, flex,
[flex, primary, widthSlider, applyWidth](bool on) {
layout::FlexItemOptions opts;
opts.flexGrow = on ? 1.0 : 0.0;
flex->setItemOptions(primary, opts);
applyWidth(widthSlider->value());
});
QObject::connect(widthSlider, &basicinput::Slider::valueChanged, widthLabel, [widthLabel, applyWidth](int w) {
widthLabel->setText(QStringLiteral("Container width: %1 px").arg(w));
applyWidth(w);
});
}
};
} // namespace fluent::galleryimport fluentqt
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget
root = QWidget()
vbox = QVBoxLayout(root)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.setSpacing(10)
controls = QWidget(root)
ctrl_row = QHBoxLayout(controls)
ctrl_row.setContentsMargins(0, 0, 0, 0)
ctrl_row.setSpacing(10)
justify = fluentqt.ComboBox(controls)
justify.setAccessibleName("Justify content")
justify.addItems(
["FlexStart", "Center", "FlexEnd", "SpaceBetween", "SpaceAround"]
)
wrap_check = fluentqt.CheckBox("Wrap lines", controls)
wrap_check.setChecked(True)
grow_check = fluentqt.CheckBox("Grow primary (flexGrow)", controls)
grow_check.setChecked(False)
ctrl_row.addWidget(justify)
ctrl_row.addWidget(wrap_check)
ctrl_row.addWidget(grow_check)
ctrl_row.addStretch()
vbox.addWidget(controls)
width_bar = QWidget(root)
width_row = QHBoxLayout(width_bar)
width_row.setContentsMargins(0, 0, 0, 0)
width_row.setSpacing(10)
width_label = fluentqt.Label("Container width: 340 px", width_bar)
width_slider = fluentqt.Slider(Qt.Horizontal, width_bar)
width_slider.setAccessibleName("Container width")
width_slider.setRange(220, 560)
width_slider.setValue(340)
width_slider.setFixedWidth(220)
width_row.addWidget(width_label)
width_row.addWidget(width_slider)
width_row.addStretch()
vbox.addWidget(width_bar)
surface = fluentqt.Card(root)
flex = fluentqt.FlexLayout(surface)
flex.setContentsMargins(12, 12, 12, 12)
flex.setGap(8)
flex.setWrap(fluentqt.FlexLayout.Wrap.Wrap)
primary = fluentqt.Button("Primary", surface)
primary.setFluentStyle(fluentqt.Button.Accent)
flex.addWidget(primary)
flex.addWidget(fluentqt.Button("Filters", surface))
flex.addWidget(fluentqt.Button("Metrics", surface))
flex.addWidget(fluentqt.Button("Alerts", surface))
flex.addWidget(fluentqt.Button("Export", surface))
flex.addWidget(fluentqt.Button("Settings", surface))
vbox.addWidget(surface)
J = fluentqt.FlexLayout.JustifyContent
W = fluentqt.FlexLayout.Wrap
modes = [J.FlexStart, J.Center, J.FlexEnd, J.SpaceBetween, J.SpaceAround]
def apply_width(width):
surface.setFixedWidth(width)
surface.setFixedHeight(max(56, flex.heightForWidth(width)))
def on_justify(idx):
flex.setJustifyContent(modes[idx])
def on_wrap(checked):
flex.setWrap(W.Wrap if checked else W.NoWrap)
apply_width(width_slider.value())
def on_grow(checked):
opts = fluentqt.FlexItemOptions()
opts.flexGrow = 1.0 if checked else 0.0
flex.setItemOptions(primary, opts)
apply_width(width_slider.value())
def on_width(width):
width_label.setText(f"Container width: {width} px")
apply_width(width)
apply_width(340)
justify.currentIndexChanged.connect(on_justify)
wrap_check.toggled.connect(on_wrap)
grow_check.toggled.connect(on_grow)
width_slider.valueChanged.connect(on_width)API
查看声明没有匹配的 API 成员。
fluent::layout::FlexLayout
继承自 QLayout
受 CSS Flexbox 启发的布局管理器,沿主轴与交叉轴排布子控件并支持自动换行与弹性伸缩。
构造函数
属性
Direction direction排布子项的主轴方向与顺序。
读取direction()设置setDirection()变化信号directionChanged()Wrap wrap主轴空间不足时的换行模式。
读取wrap()设置setWrap()变化信号wrapChanged()JustifyContent justifyContent子项与剩余空间沿主轴的对齐分布方式。
读取justifyContent()设置setJustifyContent()变化信号justifyContentChanged()AlignItems alignItems单行内子项沿交叉轴的对齐方式。
读取alignItems()设置setAlignItems()变化信号alignItemsChanged()AlignContent alignContent多行布局时各行沿交叉轴的分布方式。
读取alignContent()设置setAlignContent()变化信号alignContentChanged()int rowGap相邻行之间的垂直间距(逻辑像素)。
读取rowGap()设置setRowGap()变化信号rowGapChanged()int columnGap相邻列之间的水平间距(逻辑像素)。
读取columnGap()设置setColumnGap()变化信号columnGapChanged()
方法
Direction direction() const排布子项的主轴方向与顺序。
void setDirection(Direction direction)排布子项的主轴方向与顺序。
Wrap wrap() const主轴空间不足时的换行模式。
void setWrap(Wrap wrap)主轴空间不足时的换行模式。
JustifyContent justifyContent() const子项与剩余空间沿主轴的对齐分布方式。
void setJustifyContent(JustifyContent justify)子项与剩余空间沿主轴的对齐分布方式。
AlignItems alignItems() const单行内子项沿交叉轴的对齐方式。
void setAlignItems(AlignItems align)单行内子项沿交叉轴的对齐方式。
AlignContent alignContent() const多行布局时各行沿交叉轴的分布方式。
void setAlignContent(AlignContent align)多行布局时各行沿交叉轴的分布方式。
int rowGap() const相邻行之间的垂直间距(逻辑像素)。
void setRowGap(int gap)相邻行之间的垂直间距(逻辑像素)。
int columnGap() const相邻列之间的水平间距(逻辑像素)。
void setColumnGap(int gap)相邻列之间的水平间距(逻辑像素)。
void setGap(int gap)同时设置相同的行间距与列间距。
void addWidget(QWidget* widget, const FlexItemOptions& options)添加子控件并应用对应的弹性布局参数。
FlexItemOptions itemOptions(const QWidget* widget) const返回指定子控件对应的弹性参数;若未配置则返回默认值。
信号
void directionChanged(Direction direction)排布子项的主轴方向与顺序。
void wrapChanged(Wrap wrap)主轴空间不足时的换行模式。
void justifyContentChanged(JustifyContent justifyContent)子项与剩余空间沿主轴的对齐分布方式。
void alignItemsChanged(AlignItems alignItems)单行内子项沿交叉轴的对齐方式。
void alignContentChanged(AlignContent alignContent)多行布局时各行沿交叉轴的分布方式。
void rowGapChanged(int rowGap)相邻行之间的垂直间距(逻辑像素)。
void columnGapChanged(int columnGap)相邻列之间的水平间距(逻辑像素)。
枚举
enum class Direction主轴流动方向。行按父控件的阅读方向排列。
Row = 0RowReverse = 1Column = 2ColumnReverse = 3
enum class Wrap换行行为模式。
NoWrap = 0Wrap = 1WrapReverse = 2
enum class JustifyContent主轴内容对齐与空白分配模式。
FlexStart = 0FlexEnd = 1Center = 2SpaceBetween = 3SpaceAround = 4SpaceEvenly = 5
enum class AlignItems行内元素沿交叉轴的对齐方式。
Stretch = 0FlexStart = 1FlexEnd = 2Center = 3
enum class AlignContent多行内容沿交叉轴的整体分布方式。
Stretch = 0FlexStart = 1FlexEnd = 2Center = 3SpaceBetween = 4SpaceAround = 5
类型与别名
using ItemOptions = FlexItemOptions为 C++ 调用方保留的 FlexItemOptions 类型别名。
析构函数
Qt 基类文档:QLayout
fluent::layout::FlexItemOptions
FlexLayout 单个子项的弹性系数、基准尺寸、排序优先级与单独交叉轴对齐配置。