【Qt QML】ComboBox 组合框

由于Qt原生ComboBox不够好看,而FluentUI的FluComboBox功能不够强大,于是基于FluComboBox进行了定制(主要是不想设置风格样式)。对于Qt原生ComboBox,修改一下代码中的名称也是可以直接用的。

 下载与配置FluentUIicon-default.png?t=O83Ahttps://mp.youkuaiyun.com/mp_blog/creation/editor/141967374

以下是新ComboBox的功能与对应功能的代码片段,文末展示完整代码。

 1、用户输入提示

FluComboBox {

    // 输入提示
    property string placeholderText
    // 初始化PlaceholderText控件时需要该属性
    property variant effectiveHorizontalAlignment: contentItem.effectiveHorizontalAlignment

    id: comboBox

    PlaceholderText {
        x: contentItem.x + contentItem.leftPadding
        y: contentItem.y + contentItem.topPadding
        width: contentItem.width - (contentItem.leftPadding + contentItem.rightPadding)
        height: contentItem.height - (contentItem.topPadding + contentItem.bottomPadding)
        text: placeholderText
        font: contentItem.font
        color: contentItem.palette.placeholderText
        verticalAlignment: contentItem.verticalAlignment
        visible: comboBox.editable && !contentItem.length
                 && !contentItem.preeditText
                 && (!contentItem.activeFocus
                     || contentItem.horizontalAlignment !== Qt.AlignHCenter)
        elide: Text.ElideRight
        renderType: contentItem.renderType
    }
}

 2、指示图标随弹出框打开或关闭状态而变化

FluComboBox {
    id: comboBox
    indicator: FluIconButton {
        id: cursor
        // 按钮与组合框边框线重叠导致间隙出现,设置y与rightMargin以修正
        y: 1
        anchors.right: comboBox.right
        anchors.rightMargin: 1
        iconSize: comboBox.font.pixelSize
        iconSource: FluentIcons.ChevronDown
        onClicked: popup.open()
    }
    Connections {
        target: popup
        function onOpened() {
            if (model.count > 0)
                cursor.iconSource = FluentIcons.ChevronUp
            else {    // 无数据项时关闭
                popup.close()
            }
        }
        function onClosed() {
            cursor.iconSource = FluentIcons.ChevronDown
        }
    }
}

FluComboBox原来的指

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值