Fluent-Qt

레이아웃

FlexLayout 플렉스 레이아웃

CSS Flexbox에서 영감을 받은 QLayout 관리자로 주축/교차축 정렬, 줄바꿈 및 항목별 유연한 비율 배분을 지원합니다.

예제

FlexLayout 플렉스 레이아웃: 반응형 줄바꿈 및 유연 확장

사용법

반응형 줄바꿈 및 유연 확장

부모 위젯에 설치하고 wrap, justifyContent, 간격 및 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

상속 대상 QLayout

CSS Flexbox에서 영감을 받아 선택적 줄바꿈과 함께 주축 및 교차축으로 자식 위젯을 배치하는 레이아웃 관리자.

생성자

소멸자

열거형

enum class Direction

주축의 배치 방향입니다. 행은 부모 위젯의 읽기 방향을 따릅니다.

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

줄 간 줄바꿈 동작.

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

주축 정렬 및 간격 배치 모드.

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

한 플렉스 줄 내부에서의 교차축 항목 정렬.

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

여러 줄의 교차축 분배 모드.

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

메서드

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)

rowGap과 columnGap을 동일한 값으로 일괄 설정합니다.

void addWidget(QWidget* widget, const FlexItemOptions& options)

항목별 플렉스 옵션과 함께 위젯을 추가합니다.

void setItemOptions(QWidget* widget, const FlexItemOptions& options)

관리 중인 자식 위젯의 플렉스 옵션을 업데이트합니다.

FlexItemOptions itemOptions(const QWidget* widget) const

@p widget에 연결된 플렉스 옵션을 반환하며, 추적되지 않는 경우 기본값을 반환합니다.

속성

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

시그널

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)

연속된 열 사이의 가로 간격(논리 픽셀).

타입 및 별칭

using ItemOptions = FlexItemOptions

C++ 호출자를 위한 FlexItemOptions의 하위 호환 별칭.

Qt 기본 클래스 문서: QLayout

fluent::layout::FlexItemOptions

FlexLayout의 항목별 플렉스 크기, 정렬 순서 및 교차축 재정의 옵션.

필드

메서드