Layout
FlexLayout
CSS Flexbox-inspired QLayout managing wrapping, gaps, item order, and flexGrow/flexShrink distribution.
Example
Property preview could not load. Select Property preview to retry.

Usage
Responsive wrapping and flex grow
Install FlexLayout on a parent widget and configure wrap, justifyContent, gaps, and per-item ItemOptions.
#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
DeclarationNo matching API members.
fluent::layout::FlexLayout
Inherits QLayout
CSS Flexbox-inspired layout manager arranging child widgets across main and cross axes with optional wrapping.
Constructors
Properties
Direction directionPrimary axis and ordering direction along which items are placed.
Readdirection()WritesetDirection()NotifydirectionChanged()Wrap wrapLine-wrapping mode when items exceed available main-axis space.
Readwrap()WritesetWrap()NotifywrapChanged()JustifyContent justifyContentDistribution of items and free space along the main axis.
ReadjustifyContent()WritesetJustifyContent()NotifyjustifyContentChanged()AlignItems alignItemsAlignment of items along the cross axis within each flex line.
ReadalignItems()WritesetAlignItems()NotifyalignItemsChanged()AlignContent alignContentDistribution of wrapped lines along the cross axis when extra space is available.
ReadalignContent()WritesetAlignContent()NotifyalignContentChanged()int rowGapVertical gap in logical pixels between consecutive rows.
ReadrowGap()WritesetRowGap()NotifyrowGapChanged()int columnGapHorizontal gap in logical pixels between consecutive columns.
ReadcolumnGap()WritesetColumnGap()NotifycolumnGapChanged()
Methods
Direction direction() constPrimary axis and ordering direction along which items are placed.
void setDirection(Direction direction)Primary axis and ordering direction along which items are placed.
Wrap wrap() constLine-wrapping mode when items exceed available main-axis space.
void setWrap(Wrap wrap)Line-wrapping mode when items exceed available main-axis space.
JustifyContent justifyContent() constDistribution of items and free space along the main axis.
void setJustifyContent(JustifyContent justify)Distribution of items and free space along the main axis.
AlignItems alignItems() constAlignment of items along the cross axis within each flex line.
void setAlignItems(AlignItems align)Alignment of items along the cross axis within each flex line.
AlignContent alignContent() constDistribution of wrapped lines along the cross axis when extra space is available.
void setAlignContent(AlignContent align)Distribution of wrapped lines along the cross axis when extra space is available.
int rowGap() constVertical gap in logical pixels between consecutive rows.
void setRowGap(int gap)Vertical gap in logical pixels between consecutive rows.
int columnGap() constHorizontal gap in logical pixels between consecutive columns.
void setColumnGap(int gap)Horizontal gap in logical pixels between consecutive columns.
void setGap(int gap)Sets both rowGap and columnGap uniformly.
void addWidget(QWidget* widget, const FlexItemOptions& options)Appends a widget together with per-item flex options.
void setItemOptions(QWidget* widget, const FlexItemOptions& options)Updates the flex options for a managed child widget.
FlexItemOptions itemOptions(const QWidget* widget) constReturns the flex options associated with @p widget, or defaults if untracked.
Signals
void directionChanged(Direction direction)Primary axis and ordering direction along which items are placed.
void wrapChanged(Wrap wrap)Line-wrapping mode when items exceed available main-axis space.
void justifyContentChanged(JustifyContent justifyContent)Distribution of items and free space along the main axis.
void alignItemsChanged(AlignItems alignItems)Alignment of items along the cross axis within each flex line.
void alignContentChanged(AlignContent alignContent)Distribution of wrapped lines along the cross axis when extra space is available.
void rowGapChanged(int rowGap)Vertical gap in logical pixels between consecutive rows.
void columnGapChanged(int columnGap)Horizontal gap in logical pixels between consecutive columns.
Enumerations
enum class DirectionMain-axis flow direction. Rows follow the parent widget's reading direction.
Row = 0RowReverse = 1Column = 2ColumnReverse = 3
enum class WrapWrapping behavior across lines.
NoWrap = 0Wrap = 1WrapReverse = 2
enum class JustifyContentMain-axis justification mode.
FlexStart = 0FlexEnd = 1Center = 2SpaceBetween = 3SpaceAround = 4SpaceEvenly = 5
enum class AlignItemsCross-axis item alignment inside one flex line.
Stretch = 0FlexStart = 1FlexEnd = 2Center = 3
enum class AlignContentMulti-line cross-axis distribution mode.
Stretch = 0FlexStart = 1FlexEnd = 2Center = 3SpaceBetween = 4SpaceAround = 5
Types and aliases
using ItemOptions = FlexItemOptionsBackwards-compatible alias of FlexItemOptions for C++ callers.
Destructors
Qt base class docs: QLayout
fluent::layout::FlexItemOptions
Per-item flex sizing, ordering, and cross-axis override options for FlexLayout.