qml之自定义ComboBox

写项目的时候发现,qml自带的comboBox在样式和功能上都不能满足我的要求,所以,干脆直接自己写了一个comboBox,想怎么设置都可以,为所欲为。

代码

Rectangle {
    id:comboBox
    property alias currentIndex: listviewer.currentIndex  //把currentInedx提出来,方便外部调用
    property alias currentText: currentext.text
    property string currentname: ""       //初始状态下,comboBox显示的内容,看需求自定义
    property int listheight: 100          //listview的长度和listelement的长度
    property int listelementheight: 30
    property var items: [                //listview的代理项目
        { target: "" }
    ]
    width: 180;
    height: 50;
    smooth:true;
    
    Rectangle {
        id:chosenItem
        color: t_background_color         //我在main的全局变量里自定义的风格颜色
        border.color: Qt.rgba(0.7725, 0.7725, 0.7725, 1.0)
        border.width: 1
        radius:4;
        width:parent.width;
        height:comboBox.height;
        smooth:true;

        Text {
            id:currentext
            color: t_color
            width: parent.width-parent.height
            height: parent.height
            text: comboBox.currentname
            font.family: "Arial"     //字体
            font.pointSize: parent.height/3
            smooth:true
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            visible: true;  //可见
        }

       //下拉按钮
        Button{
            width: height
            height: parent.height
            anchors.right: parent.right
            background: Rectangle{
                color: t_background_color
                border.color: Qt.rgba(0.7725, 0.7725, 0.7725, 1.0)
                border.width: 1
            }
            Text {
                id: icon
                text: qsTr("\uf150")     //调用图标库,怎么调用可以看我的另一篇博客,自定义spinBox里
                font.family: "fontawesome"
                color: t_color
                anchors.centerIn: parent
                font.pixelSize: parent.height
                anchors.right: parent.right
            }
        }

        MouseArea {
            width: comboBox.width / 18 * 40
            height: comboBox.height / 3 
            anchors.bottomMargin: 0
            anchors.fill: parent;
            onClicked: {
                comboBox.state = comboBox.state==="dropDown"?"":"dropDown"  //控制下拉和回收
            }
        }
    }

    Rectangle{
        id:dropDown
        width:comboBox.width;
        height:0;
        clip:true;  
        radius:4;
        anchors.top: chosenItem.bottom;
        anchors.margins: comboBox.height / 9  
        color: t_background_color
        border.color: Qt.rgba(0.8627, 0.8627, 0.8627, 1.0)
        border.width: 1

        ListView{
            id:listviewer
            height: comboBox.listheight;
            model: comboBox.items
            currentIndex: 0;
            delegate: Rectangle{
                id: listbg
                width: comboBox.width
                height: comboBox.listelementheight;
                color: ListView.isCurrentItem ? "lightblue" : "#00333333"  //设置当前选择项高亮
                Text {
                    text: modelData.target
                    font.pixelSize: listbg.height
                    anchors.fill: parent
                    color: t_color;
                    elide: Text.ElideRight
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                }

                MouseArea {
                    anchors.fill: parent;
                    onClicked: {
                        comboBox.state = ""
                        currentext.text = modelData.target;
                        listviewer.currentIndex = index;
                    }
                }
            }
        }
        Scrolbar{
            target: listviewer 
        }
    }

//设置下拉动画
    //状态列表
    states: State {
        name: "dropDown";
        PropertyChanges { target: dropDown; height:listviewer.height}
    }
    //转换列表
    transitions: Transition {
        NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 }
    }
}

调用

            MycomboBox{
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 500
                anchors.right: parent.right
                anchors.rightMargin: 350
                width: 400
                height: 50
                listheight: 130
                listelementheight: 30
                currentname: "开局一只鲲"
                items: [
                    {target:"唱跳"},
                    {target:"Rap"},
                    {target:"篮球"},
                    {target:"律师函警告"}
                ]
            }

效果

在这里插入图片描述
在这里插入图片描述

QML中自制ComboBox可以通过自定义控件来实现,可以使用基础的控件来模拟ComboBox的功能。根据您的需求,您可以创建一个下拉菜单控件,通过点击按钮展开或收起菜单选项。在QML中,您可以使用ListView或者Repeater来显示菜单选项,然后根据用户的选择更新显示文本。您可以使用Item来创建自定义样式,设置按钮样式、菜单选项样式等。您还可以使用属性来填充数据模型,可以使用JavaScript数组、ListModel或者整数,也支持其他类型的数据模型提供的属性。通过使用这些控件和属性,您可以根据自己的需求来自制ComboBox。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [QML 自定义ComboBox控件](https://blog.csdn.net/weixin_43892328/article/details/122615847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [QML 自制二级开合下拉菜单](https://blog.csdn.net/weixin_51019869/article/details/118146812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值