qml自定义Combobox

qml自定义具有二级菜单的Combobox

废话

由于项目需要计划做个多级菜单,后来因为项目的特殊性放弃了这种多级菜单,但是觉得这种菜单应该挺有意思,所以在闲暇时候还是做了一个具有二级菜单的Combobox。

先来看看效果

这里写图片描述

-☞表示具有有子菜单

设计思路

其实比较简单!界面效果是一个文本框和一张图片组合,加两个列表菜单组成的,具体代码中可以很清楚看到,Combobox中数据是通过ListModel填充。

上代码

控件是由纯qml完成的不涉及c++,控件部分总共用了两个qml文件(一个文件也可以,无关紧要),还有一个是main.qml是调用控件的.
ComboBoxButton.qml:

import QtQuick 2.2

FocusScope {
    id: container
    signal clicked
    property alias source: image.source
    property alias text: label.text
    property color textColor: "white"
    property color borderColor: "limegreen"
    property color backgroundColor: "transparent"
    property int borderWidth: 2
    property int textSize: 12
    property int radius: 5
    property int margin: 0

    Rectangle {
        id: buttonRectangle
        anchors.fill: parent
        color: container.backgroundColor
        radius: container.radius
        border{width: container.borderWidth; color: container.borderColor;}

        Image {
            id: image
            smooth: true
            fillMode: Image.PreserveAspectFit
            anchors{top: parent.top; right: parent.right; margins: container.margin;}
            width: parent.height; height: parent.height-2*container.margin
        }

        Item {
            anchors{top: parent.top; bottom: parent.bottom; left: parent.left; right: image.left; leftMargin: container.margin * 2;}
            Text {
                id: label
                color: container.textColor
                anchors.centerIn: parent
                font{pixelSize: container.textSize-5; bold: true;}
            }
        }

        MouseArea {
            id: mouseArea;
            anchors.fill: parent
            onClicked: {
                buttonRectangle.state = "pressed"
                container.clicked()
            }
        }

        states: State {
            name: "pressed"
            PropertyChanges { target: image; scale: 0.7 }
        }

        transitions: Transition {
            NumberAnimation { properties: "scale"; duration: 200; easing.type: Easing.InOutQuad }
        }
    }
}

Mycombobox.qml:

import QtQuick 2.2

FocusScope {
    id: comboBox
    property int maxHeight: 1000
    property alias currentIndex: listView.currentIndex
    property alias currentIndex2: delegatelistview.currentIndex
    property alias buttonTextSize: comboBoxButton.textSize
    property alias text: comboBoxButton.text
    property bool readOnly: false
    property variant listModel
    property int beforeDropIndex:0
    property int beforeDropIndex2: 0
    property color textColor: "white"
    property color borderColor: "olivedrab"
    property color backgroundColor: "#FF00688A"
    property color focus_borderColor: "lightsalmon"
    property color focus_backgroundColor: "#FF00688A"
    property color focus_textColor: "darkred"
    signal comboBoxSelected(int idx,int index)
    signal comboBoxTextChanged(string comboxtext)
    width: 160; height: 32
    z: listBackground.height===0 ? 0 : 100

    ComboBoxButton {
        id: comboBoxButton
        anchors.fill: parent;
        source: "ico/down.png"
        text: typeof(listModel) == "undefined"? "":typeof( listModel.get(currentIndex).attributes)=="undefined"?listModel.get(comboBox.currentIndex).name:listModel.get(currentIndex).attributes.get(currentIndex2).description
        textSize: parent.height-4
        textColor: comboBox.activeFocus ? comboBox.focus_textColor : comboBox.textColor
        borderColor: comboBox.activeFocus ? comboBox.focus_borderColor : comboBox.borderColor
        backgroundColor: comboBox.activeFocus ? comboBox.focus_backgroundColor : comboBox.backgroundColor
        onClicked: comboxClick()
    }
    Component {
        id: comboBoxDelegate
        Item {
            id:comitem
            width: parent.width; height: comboBoxButton.height;
            Text {
                id:comtext
                color: index == listView.currentIndex ? "#ffff00" : "white"
                anchors.centerIn: parent
                font{pixelSize: comboBoxButton.textSize-5; bold: true;}
                text:name          }
            Text{
                id:icotext
                color:"white"
                text:typeof(listModel.get(index).attributes) == "undefined"?"":"☞";
                font{pixelSize: comboBoxButton.textSize-5; bold: true;}
                anchors{top:parent.top;right: parent.right}
                verticalAlignment: TextInput.AlignVCenter
            }
            MouseArea {
                anchors.fill: parent
                onClicked: comboBox.comboxSelect(index);
            }
            onFocusChanged: {delegatelistview.visible=true;}
        }


    }
    Component {
        id: delegate
        Item {
            width: parent.width; height: comboBoxButton.height;
            Text {
                id:comtext
                color:"white"
                anchors.centerIn: parent
                font{pixelSize: comboBoxButton.textSize-5; bold: true;}
                text:description;
            }
            MouseArea {
                anchors.fill: parent
                onClicked: comboBox.comboxSelect2(index);
            }
            onFocusChanged: {delegatelistview.visible=true;}
            Keys.onPressed:
            {
                if(event.key===Qt.Key_Enter||event.key===Qt.Key_Return)
                {
                    comboBox.comboxClick();
                }
            }

        }

    }

    Rectangle{
        id:delegateback
        x:listView.x+listView.width
        y:listBackground.y+listView.currentIndex*listView.highlightItem.height
        color:"#FF000000"
        radius: comboBoxButton.radius
        visible: false
        border{width: comboBoxButton.borderWidth; color: comboBoxButton.borderColor;}
        ListView
        {
            id:delegatelistview
            anchors.fill: parent
            visible:false
            clip:true
            delegate: delegate
            keyNavigationWraps :true
            highlight: Rectangle{color: delegatelistview.focus?"#FF00688A":"transparent"; radius: 5;}
        }
        Behavior on height {
            NumberAnimation {property: "height"; duration: 200 }
        }
        state: "Hide"
        states: [
            State {
                name: "Hide"
                PropertyChanges {target: delegateback; visible:false;height:0;width:0}
                StateChangeScript{
                    script: {
                        delegatelistview.focus=false
                    }
                }
            },
            State {
                name: "Show"
                PropertyChanges {target: delegateback;visible:typeof(delegatelistview.model) == "undefined"?false:true; focus: false;height: Math.min(maxHeight, typeof(delegatelistview.model) == "undefined"?0:delegatelistview.model.count*comboBoxButton.height);width:comboBoxButton.width}
                StateChangeScript{
                    script: {
                    }
                }
            }
        ]
    }
    ListModel{id:model}
    Rectangle {
        id: listBackground
        anchors{top: comboBoxButton.bottom; left: comboBoxButton.left;}
        width: parent.width
        color: "#FF000000"
        radius: comboBoxButton.radius
        border{width: comboBoxButton.borderWidth; color: comboBoxButton.borderColor;}
        ListView {
            id: listView
            anchors.fill: parent
            clip: true
            focus:true
            model:listModel
            delegate: comboBoxDelegate
            highlight: Rectangle{color:"#FF00688A"; radius: 5;}
            onCurrentIndexChanged: {delegatelistview.model=listModel.get(currentIndex).attributes;}
        }

        Behavior on height {
            NumberAnimation {property: "height"; duration: 200 }
        }
    }
    function comboxSelect2(index)
    {
        currentIndex2=index
        console.log(index)
        state="Close"
    }
    function comboxSelect(index){
        currentIndex = index
        state = "Close"
    }
    function comboxClick(){
        if(readOnly) return
        forceActiveFocus()
        if (state == "Close"){
            state = "Drop"
            delegatelistview.model=listModel.get(currentIndex).attributes
        }
        else{
            state = "Close"
            delegateback.state="Hide"
        }
    }
    onActiveFocusChanged: { if(!activeFocus) state = "Close" }
    onStateChanged: {if(state=="Close")delegateback.state="Hide"}
    state: "Close"
    states: [
        State {
            name: "Close"
            PropertyChanges {target: listBackground; height: 0}
            StateChangeScript{
                script: {
                    if(currentIndex===beforeDropIndex&&currentIndex2===beforeDropIndex2)
                        return
                    comboBoxSelected(currentIndex,currentIndex2)
                    comboBoxTextChanged(text)
                    delegateback.state="Hide"
                }
            }
        },
        State {
            name: "Drop"
            PropertyChanges {target: listBackground; height: Math.min(maxHeight, listModel.count*comboBoxButton.height); focus: true}
            StateChangeScript{
                script: {
                    beforeDropIndex = currentIndex
                    beforeDropIndex2=currentIndex2
                    delegateback.state="Show"
                }
            }
        }
    ]


    Keys.onUpPressed: {
        if(state == "Drop"){ listView.decrementCurrentIndex(); return}
        event.accepted = false
    }
    Keys.onDownPressed: {
        if(state == "Drop"){ listView.incrementCurrentIndex(); return}
        event.accepted = false
    }
    Keys.onReturnPressed: {
        if(typeof(delegatelistview.model) == "undefined")
        {
            comboxClick();
            return}
        listView.focus=false;
        delegatelistview.forceActiveFocus();
        event.accepted = false
    }
    Keys.onEnterPressed: {
        if(typeof(delegatelistview.model) == "undefined")
        {
            comboxClick();
            return}
        listView.focus=false;
        delegatelistview.forceActiveFocus();
        event.accepted = false}
    Keys.onRightPressed: {if(typeof(delegatelistview.model) == "undefined")return;listView.focus=false;delegatelistview.forceActiveFocus();event.accepted = false}
    Keys.onLeftPressed: {if(delegatelistview.focus)listView.forceActiveFocus();event.accepted = false;}
    Keys.onPressed: {
        if(event.key === Qt.Key_Escape){
            if(state !== "Close"){
                state = "Close"
                event.accepted = true
            }
        }
    }
}

main.qml:

import QtQuick 2.3
import QtQuick.Window 2.2
import "./"
Window {
    visible: true

    MouseArea {
        anchors.fill: parent
        onClicked: {
           // Qt.quit();
        }
    }
    Mycombobox{listModel: fruitModel;anchors.centerIn: parent}
    ListModel {
        id: fruitModel

        ListElement {
            name: "Apple"
            attributes: [
                ListElement { description: "Core" },
                ListElement { description: "Deciduous" }
            ]
        }
        ListElement {
            name: "Orange"
            attributes: [
                ListElement { description: "Citrus" }
            ]
        }
        ListElement {
            name: "mango"
        }
        ListElement {
            name: "Banana"
            attributes: [
                ListElement { description: "Tropical" },
                ListElement { description: "Seedless" },
                ListElement { description: "Yellow" }
            ]
        }

    }
}


  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值