Fluent-Qt

Layout

FlexLayout

CSS Flexbox-inspired QLayout managing wrapping, gaps, item order, and flexGrow/flexShrink distribution.

Example

FlexLayout: Responsive wrapping, justification, and flexGrow

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::gallery

fluent::layout::FlexLayout

Inherits QLayout

CSS Flexbox-inspired layout manager arranging child widgets across main and cross axes with optional wrapping.

Constructors

Properties

Direction direction

Primary axis and ordering direction along which items are placed.

Read direction()Write setDirection()Notify directionChanged()
Wrap wrap

Line-wrapping mode when items exceed available main-axis space.

Read wrap()Write setWrap()Notify wrapChanged()
JustifyContent justifyContent

Distribution of items and free space along the main axis.

Read justifyContent()Write setJustifyContent()Notify justifyContentChanged()
AlignItems alignItems

Alignment of items along the cross axis within each flex line.

Read alignItems()Write setAlignItems()Notify alignItemsChanged()
AlignContent alignContent

Distribution of wrapped lines along the cross axis when extra space is available.

Read alignContent()Write setAlignContent()Notify alignContentChanged()
int rowGap

Vertical gap in logical pixels between consecutive rows.

Read rowGap()Write setRowGap()Notify rowGapChanged()
int columnGap

Horizontal gap in logical pixels between consecutive columns.

Read columnGap()Write setColumnGap()Notify columnGapChanged()

Methods

Direction direction() const

Primary 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() const

Line-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() const

Distribution 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() const

Alignment 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() const

Distribution 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() const

Vertical gap in logical pixels between consecutive rows.

void setRowGap(int gap)

Vertical gap in logical pixels between consecutive rows.

int columnGap() const

Horizontal 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) const

Returns 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 Direction

Main-axis flow direction. Rows follow the parent widget's reading direction.

  • Row = 0
  • RowReverse = 1
  • Column = 2
  • ColumnReverse = 3
enum class Wrap

Wrapping behavior across lines.

  • NoWrap = 0
  • Wrap = 1
  • WrapReverse = 2
enum class JustifyContent

Main-axis justification mode.

  • FlexStart = 0
  • FlexEnd = 1
  • Center = 2
  • SpaceBetween = 3
  • SpaceAround = 4
  • SpaceEvenly = 5
enum class AlignItems

Cross-axis item alignment inside one flex line.

  • Stretch = 0
  • FlexStart = 1
  • FlexEnd = 2
  • Center = 3
enum class AlignContent

Multi-line cross-axis distribution mode.

  • Stretch = 0
  • FlexStart = 1
  • FlexEnd = 2
  • Center = 3
  • SpaceBetween = 4
  • SpaceAround = 5

Types and aliases

using ItemOptions = FlexItemOptions

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

Methods

Fields