• 5099阅读
  • 2回复

[提问]qml中 combobox 中怎么嵌入checkbox [复制链接]

上一主题 下一主题
离线z_墨脱
 

只看楼主 倒序阅读 楼主  发表于: 2014-12-23
RT
离线彩阳

只看该作者 1楼 发表于: 2014-12-27
看看可以自己制作一个ComboBox类,来实现添加CheckBox。
上海Qt开发联盟,热忱地欢迎你的加入!

只看该作者 2楼 发表于: 2015-12-04
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
    id:comboBox
    property variant items: ["100", "45", "63", "123","23","100", "45", "63", "123","23","100", "45", "63", "123","23"]
    property alias selectedItem: chosenItemText.text;
    property alias selectedIndex: listView.currentIndex;
    property var selectedname: []


    signal comboClicked;
    width: 110;
    height: 30;
    smooth:true;
    function addnum(num){


        var numindex = selectedname.indexOf(num)
        if(numindex!=-1){

            //添加时如果已经有值则不处理

        }
        else{

            //添加时如果没有值则添加
            selectedname.push(num)

        }

    }
    function deletenum(num){


        var numindex = selectedname.indexOf(num)
        if(numindex!=-1){


           //删除时如果有值则删除
            selectedname.splice(numindex,1)//删除指定位置的一个数值

        }
        else{

            //删除时如果没有值则不处理

        }

    }


    Rectangle {
        id:chosenItem
        radius:4;
        width:parent.width;
        height:comboBox.height;
        color: "#2EB3FF"
        smooth:true;
        Text {
            anchors.top: parent.top;
            anchors.left: parent.left;
            anchors.margins: 3;
            anchors.verticalCenter: parent.verticalCenter
            id:chosenItemText
            text:comboBox.items[0];
            font.family: "Arial"
            font.pointSize: 14;
            smooth:true
        }
        MouseArea {
            anchors.fill: parent;
            onClicked: {
                comboBox.state = comboBox.state==="dropDown"?"":"dropDown"
            }
        }
    }
    Rectangle {
        id:dropDown
        width:comboBox.width;
        height:0
        clip:true;//剪切子元素
        radius:0
        anchors.top: chosenItem.bottom;
        anchors.topMargin: 2
        color: "lightgray"

        ListView {
            id:listView
            clip: true
            //interactive:false
            height:250
            width: dropDown.width-4
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: dropDown.top
            anchors.topMargin: 4
            z:1
            model: comboBox.items
            currentIndex: 0
            delegate: Item{
                width:listView.width;
                height: comboBox.height;
                Rectangle {
                    id: mouseMoveHighLight
                    width:listView.width;
                    height:comboBox.height;
                    color: "green";
                    opacity: 0
                    radius: 4
                    z:0

                }

                CheckBox {

                    id: checkBox1
                    anchors.top: parent.top;
                    anchors.left: parent.left;
                    anchors.margins: 5;
                    text: modelData


                    onCheckedStateChanged: {

                        if(checked){

                            addnum(modelData)

                        }
                        else{

                            deletenum(modelData)
                        }

                        console.log(selectedname);
                       // selectedItem = selectedname
                        chosenItemText.text = selectedname.toString()
                    }



                }
                MouseArea {
                    anchors.fill: parent;
                    hoverEnabled: true
                    onClicked: {

                        checkBox1.checked = !checkBox1.checked
                        listView.currentIndex = index;
                    }
                    onEntered: mouseMoveHighLight.opacity = 0.5;
                    onExited: mouseMoveHighLight.opacity = 0;
                }
            }
            highlight: Rectangle {
                width:listView.width;
                height:comboBox.height;
                color: "green";
                radius: 4
            }
        }
        ScrollBar  {
            scrollArea: listView
            height: listView.height
            width: 10
            anchors.right: listView.right
        }

    }
    states: State {
        name: "dropDown";
        PropertyChanges { target: dropDown; height:260 }
    }
    transitions: Transition {
        NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 200 }
    }
}
快速回复
限100 字节
 
上一个 下一个