QtShadcn
组件

ShadcnSelect

下拉选择:trigger + chevron-down + popover 弹层(hover accent / 选中 check)。

基于 QQC.ComboBox(Basic style)定制,对齐 shadcn select 规范。

用法

ShadcnSelect {
    model: [qsTr("苹果"), qsTr("香蕉"), qsTr("橙子")]
    onActivated: (index) => console.log(currentText)
}

// 小尺寸 / 禁用
ShadcnSelect { size: ShadcnSelect.Size.Sm; model: ["S", "M", "L"] }
ShadcnSelect { model: ["不可用"]; enabled: false }

属性

属性类型说明
modelvar选项(字符串列表 / 对象列表 + textRole)
sizeenumSize.Default(32px, h-8) / Size.Sm(28px, h-7)
currentIndexint当前选中(继承 QQC.ComboBox)

视觉

  • trigger:bg-input/50 + rounded-lg(sm 档 min(radiusMd,10px))+ 左右 10px padding(px-2.5);右侧 chevron-down(16px mutedForeground)
  • 弹层:bg-popover + 1px 环 + shadow + fade/zoom 动画,item hover accent + 选中项右侧 check 图标
  • 聚焦:border-ring + 3px 外环
  • 键盘:上下方向键 + 回车选择

数据模型

四种来源都可以直接用,不需要预先转换(0.2.0 起,此前 C++ 容器 + textRole 会显示成 undefined):

ShadcnSelect { model: ["苹果", "香蕉", "橙子"] }        // JS 字符串数组

ShadcnSelect {                                        // JS 对象数组 + textRole
    textRole: "name"
    model: [{ name: "甲" }, { name: "乙" }]
}

ShadcnSelect {                                        // ListModel
    textRole: "label"
    model: ListModel { ListElement { label: "甲" } }
}

// C++ 侧 Q_PROPERTY(QVariantList labels ...),textRole 指向字段名
ShadcnSelect { textRole: "name"; model: backend.labels }

textRole 为空时按整个 modelData 转字符串;设了就从该条记录的 textRole 字段取。

相关

  • 与 shadcn Select/Combobox 对应;需要可编辑输入的下拉暂未支持
Copyright © 2026