HusSegmented 分段控制器

August 16, 2026 · View on GitHub

← 返回主目录

← 返回本类别目录

HusSegmented 分段控制器

用于展示多个选项并允许用户选择其中单个选项。

  • 模块 { HuskarUI.Basic }

  • 继承自 { Control }


支持的代理:

  • indicatorDelegate: Component 指示器代理。

  • itemDelegate: Component 项代理,代理可访问属性:

    • index: int 模型数据索引

    • model: var 模型数据

    • pressed: bool 是否按下

    • hovered: bool 是否悬浮

    • isCurrent: bool 是否为当前项

  • iconDelegate: Component 内容代理,代理可访问属性:

    • index: int 模型数据索引

    • model: var 模型数据

    • pressed: bool 是否按下

    • hovered: bool 是否悬浮

    • isCurrent: bool 是否为当前项

  • toolTipDelegate: Component 文本提示代理,代理可访问属性:

    • index: int 模型数据索引

    • model: var 模型数据

    • pressed: bool 是否按下

    • hovered: bool 是否悬浮

    • isCurrent: bool 是否为当前项


支持的属性:

属性名类型默认值描述
animationEnabledboolHusTheme.animationEnabled是否开启动画
optionsarray[]选项模型列表
currentIndexint0当前索引
currentValuevar(readonly)-当前值
countint(readonly)-选项数量
blockboolfalse是否填充父元素整行
orientationenumQt.Horizontal方向(来自 Qt.*)
defaultItemHeightreal26默认项高度
iconSpacingint5图标间隔
iconFontfont-图标字体
colorBgcolor-背景颜色
colorIndicatorBgcolor-指示器颜色
radiusBgHusRadius-背景圆角
borderBgHusBorder-背景边框
sizeHintstring'normal'尺寸提示

选项支持的属性:

属性名类型可选/必选描述
labelstring必选标签文本
valuevar可选值(默认为label)
enabledbool可选是否启用
toolTipstring可选文本提示
iconSourceint丨string可选本标签的图标源

支持的函数:

  • get(index: int): Object 获取 index 处的选项数据

  • set(index: int, object: Object) 设置 index 处的选项数据为 object

  • setProperty(index: int, propertyName: string, value: any) 设置 index 处的选项数据属性名 propertyName 值为 value

  • move(from: int, to: int, count: int = 1)count 个选项数据从 from 位置移动到 to 位置

  • insert(index: int, object: Object) 插入选项 objectindex

  • append(object: Object) 在末尾添加选项 object

  • remove(index: int, count: int = 1) 删除 indexcount 个选项数据

  • clear() 清空所有选项数据



代码演示

示例 1 - 基本

最简单的用法。

通过 options 属性设置选项数据,数据项可以为字符串。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    HusRadioBlock {
        id: sizeHintRadio
        initCheckedIndex: 1
        model: [
            { label: 'Small', value: 'small' },
            { label: 'Normal', value: 'normal' },
            { label: 'Large', value: 'large' },
        ]
    }

    HusSegmented {
        sizeHint: sizeHintRadio.currentCheckedValue
        options: ['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']
    }

    HusSegmented {
        sizeHint: sizeHintRadio.currentCheckedValue
        options: [
            { label: 'List', value: 'List', iconSource: HusIcon.BarsOutlined },
            { label: 'Kanban', value: 'Kanban', iconSource: HusIcon.AppstoreOutlined }
        ]
    }
}

示例 2 - 垂直方向

通过 orientation 属性改变方向,支持的方向:

  • 水平方向(默认){ Qt.Horizontal }

  • 垂直方向{ Qt.Vertical }

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    HusSegmented {
        id: customSegmented
        width: 100
        borderBg.color: '#77BEF0'
        orientation: Qt.Vertical
        options: [
            {
                label: 'Boost ',
                toolTip: 'Boost',
                iconSource: HusIcon.RocketOutlined,
            },
            {
                label: 'Stream',
                toolTip: 'Stream',
                iconSource: HusIcon.ThunderboltOutlined,
            },
            {
                label: 'Cloud ',
                toolTip: 'Cloud',
                iconSource: HusIcon.CloudOutlined,
            }
        ]
        iconDelegate: HusIconText {
            font: customSegmented.iconFont
            colorIcon: '#77BEF0'
            iconSource: model.iconSource
        }
    }
}

示例 3 - 填充整行

通过 block 属性使其填充父元素宽度。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    HusSegmented {
        block: true
        options: [123, 456, 'longtext-longtext-longtext-longtext']
    }
}

示例 4 - 启用/禁用

通过 options.enabled 属性启用/禁用项。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    HusSegmented {
        enabled: false
        options: ['Map', 'Transit', 'Satellite']
    }

    HusSegmented {
        options: [
            'Daily',
            { label: 'Weekly', value: 'Weekly', enabled: false },
            'Monthly',
            { label: 'Quarterly', value: 'Quarterly', enabled: false },
            'Yearly',
        ]
    }
}

示例 5 - 胶囊形状

胶囊型的 HusSegmented。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    HusSegmented {
        options: ['small', 'middle', 'large']
    }

    HusSegmented {
        options: [
            { iconSource: HusIcon.SunOutlined },
            { iconSource: HusIcon.MoonOutlined },
        ]
        radiusBg.all: height * 0.5
    }
}