Fluent-Qt

菜单与工具栏

CommandBar 命令栏

排列常用命令,放不下的命令移到溢出菜单。

示例

命令栏的真实 Gallery 示例

用法

自适应命令栏

宽度不够时,低优先级的命令先移入溢出菜单;setLabelPosition() 控制文字显示的位置。

#include <FluentQt/MenusToolbars.h>
using namespace fluent::menus_toolbars;

auto* barHost = new QWidget(this);
barHost->setFixedWidth(536);
auto* barLayout = new QHBoxLayout(barHost);
barLayout->setContentsMargins(0, 0, 0, 0);
auto* bar = new CommandBar(barHost);
bar->setAccessibleName("Document commands");
bar->setLabelPosition(
    CommandBar::LabelPosition::Right);
bar->setBackgroundVisible(false);
barLayout->addWidget(bar);

auto* addAction = new QAction(
    QIcon(":/icons/add.svg"), "Add", this);
auto* editAction = new QAction(
    QIcon(":/icons/edit.svg"), "Edit", this);
editAction->setPriority(QAction::HighPriority);
auto* shareAction = new QAction(
    QIcon(":/icons/share.svg"), "Share", this);
auto* separator = new QAction(this);
separator->setSeparator(true);
auto* syncAction = new QAction(
    QIcon(":/icons/sync.svg"), "Sync", this);
syncAction->setPriority(QAction::LowPriority);
auto* pinAction = new QAction(
    QIcon(":/icons/pin.svg"), "Pin", this);
pinAction->setCheckable(true);

bar->addPrimaryAction(addAction);
bar->addPrimaryAction(editAction);
bar->addPrimaryAction(shareAction);
bar->addPrimaryAction(separator);
bar->addPrimaryAction(syncAction);
bar->addPrimaryAction(pinAction);
bar->addSecondaryAction(new QAction(
    QIcon(":/icons/settings.svg"), "Settings", this));
bar->addSecondaryAction(new QAction(
    QIcon(":/icons/help.svg"), "Help", this));

auto* compactButton =
    new Button("Compact view", this);
auto* labelsButton =
    new Button("Labels: Right", this);
auto* backgroundButton =
    new Button("Show background", this);
connect(compactButton, &Button::clicked, barHost,
        [barHost] {
            barHost->setFixedWidth(
                barHost->width() > 300 ? 288 : 536);
        });
connect(labelsButton, &Button::clicked, bar, [=] {
    const bool showLabels =
        bar->labelPosition()
        == CommandBar::LabelPosition::Collapsed;
    bar->setLabelPosition(
        showLabels
            ? CommandBar::LabelPosition::Right
            : CommandBar::LabelPosition::Collapsed);
});
connect(backgroundButton, &Button::clicked, bar, [=] {
    bar->setBackgroundVisible(
        !bar->backgroundVisible());
});
using fluent::status_info::Toast;
for (QAction* action : bar->primaryActions() + bar->secondaryActions()) {
    if (action->isSeparator()) continue;
    connect(action, &QAction::triggered, this, [this, action] {
        QString text = action->text().section('\t', 0, 0).remove('&');
        Toast::showOrUpdateToast(this, "gallery.action-feedback", text + " selected",
            Toast::Informational, 2200, Toast::Top, QMargins(16, 50, 16, 16));
    });
}

fluent::menus_toolbars::CommandBar

继承自 QWidget · FluentElement · QMLPlus

承载有序主命令和次命令的应用命令栏。

构造函数

属性

LabelPosition labelPosition
读取 labelPosition()设置 setLabelPosition()通知 labelPositionChanged()
bool dynamicOverflowEnabled
读取 isDynamicOverflowEnabled()设置 setDynamicOverflowEnabled()通知 dynamicOverflowEnabledChanged()
bool overflowOpen
读取 isOverflowOpen()设置 setOverflowOpen()通知 overflowOpenChanged()
bool backgroundVisible
读取 backgroundVisible()设置 setBackgroundVisible()通知 backgroundVisibleChanged()

方法

void addAction(QAction* action)

通过 Qt 控件 API 将 QAction 添加为主命令。

void insertAction(QAction* before, QAction* action)

将新的 Qt 控件动作插入主命令顺序。

void removeAction(QAction* action)

移除 Qt 控件关联和语义命令成员关系。

bool addPrimaryAction(QAction* action)

将借用的动作追加到主命令分区。

bool insertPrimaryAction(QAction* before, QAction* action)

在指定主命令之前插入借用的主命令。

bool addSecondaryAction(QAction* action)

将借用的动作追加到次命令分区。

bool insertSecondaryAction(QAction* before, QAction* action)

在指定次命令之前插入借用的次命令。

bool removeCommandAction(QAction* action)

从任一语义分区移除动作,但不删除动作。

void clearPrimaryActions()

清空主命令成员关系,但不删除动作。

void clearSecondaryActions()

清空次命令成员关系,但不删除动作。

QList<QAction*> primaryActions() const

按语义顺序返回主命令。

QList<QAction*> secondaryActions() const

按语义顺序返回次命令。

QList<QAction*> overflowedPrimaryActions() const

返回当前仅用于呈现的溢出快照。

bool isPrimaryActionOverflowed(const QAction* action) const

返回指定主命令当前是否进入溢出区。

void setOverflowOpen(bool open)

在存在可用溢出内容时打开或关闭溢出呈现。

信号

枚举

enum class LabelPosition

控制内联命令标签是否显示在图标右侧。

  • Collapsed
  • Right

类型与别名

析构函数

子类扩展接口

方法

继承自 fluent::FluentElement

方法

static void setTheme(Theme theme)

更新全局主题,并通知所有活跃的 FluentElement 实例。

static void setThemeDeferred(Theme theme)

以可见元素优先的批次调度主题刷新,避免阻塞当前交互。

static Theme currentTheme()

返回当前生效的全局主题。

static bool themeUsesDarkAppearance(Theme theme)

返回指定主题是否使用暗色底的 chrome 与材质 token。

static void refreshTheme()

在设计 token 变化后重新广播当前主题以触发重绘。

static int themeGeneration()

每次主题变化自增的单调计数器,用于过期判断。

Theme effectiveTheme() const

解析此元素实际使用的主题,会优先读取 QWidget 父链上的局部主题覆盖。

bool effectiveThemeUsesDarkAppearance() const

返回此元素的实际主题是否使用暗色底 chrome。

bool setThemeOverrides(const QJsonObject& overrides)

供组件侧访问的设计 token 接口。

QJsonObject themeOverrides() const

返回当前元素的覆盖配置。

void clearThemeOverrides()

恢复全部 token 跟随全局主题。

const Colors& themeColorsRef() const

对当前色板的零拷贝访问(用于绘制热路径)。

Material::AcrylicToken themeAcrylic() const

材质、阴影和自适应布局 token。

QColor themeBackdrop(bool active) const

给定激活状态下的窗口 chrome 背景色。

virtual void onThemeUpdated()

全局主题变化后调用。

枚举

enum Theme

解析设计 token 时使用的全局视觉主题。

  • Light = 0
  • Dark = 1
  • HighContrast = 2

类型与别名

struct Colors

返回给组件使用的设计 token 快照。

继承自 fluent::QMLPlus

方法

AnchorLayout::Anchors* anchors()

返回 QWidget 宿主的可变锚点配置。

void setState(const QString& name)

应用具名 state;名称为空时恢复默认状态。

void addState(const QMLState& state)

注册或替换一个具名 state 定义。

void evaluateStates()

重新计算条件;按注册顺序选择首个成立的条件。

QMLPlusRuntime* runtime()

返回按需创建、由宿主持有的状态观察对象。

void setAnchors(const AnchorLayout::Anchors& anchors)

替换宿主的全部锚点,并使父布局失效。

void resetAnchors()

清除宿主全部锚点,并使父布局失效。

void addTransition(const StateTransition& transition)

按优先顺序注册状态过渡。

void setBehavior(const char* propertyName, const PropertyAnimation& animation)

设置宿主受管理属性写入时使用的默认动画。

void clearBehavior(const char* propertyName)

移除后续受管理写入使用的默认动画。

bool writeProperty(const char* propertyName, const QVariant& value)

通过 Behavior 写入宿主属性,且不移除该属性的绑定。

Qt 基类完整成员:QWidget