QML ComboBox 图片加文字

Qml 目前版本的 ComboBox,不支持设置下拉菜单字体,也不支持下拉菜单带图片。在一番搜索加调整后,我写成下面的代码,可以满足这个功能。
只是调整字体,请见ComboxBox 调整字体

Rectangle {
    id:comboBox
    property var items: [
{ lang: "en", img: "img/European-Union-icon.png" },
{ lang: "sp", img: "img/Brazil-icon.png" },
{ lang: "fr", img: "img/Jamaica-icon.png" }
    ]
    signal comboClicked;
    anchors.top: main.top;
    anchors.topMargin: 30;
    anchors.left: main.left;
    anchors.leftMargin: 415;
    width: 60;
    height: 60;
    smooth:true;

    Rectangle {
        id:chosenItem
        radius:4;
        width:parent.width;
        height:comboBox.height;
        smooth:true;

        Image {
            id: main_img;
            fillMode: Image.PreserveAspectFit
            source: "image/tunnel_arch.png";
            height: 100
            width:  100
        }

        Text {
            anchors.centerIn: parent;
            id:chosenItemText
            x: 11
            y: 5
            color: "#ffffff"
            text:"ENG";
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 12
            font.family: "Arial"
            font.pointSize: 30;
            smooth:true
            visible: false;
        }

        MouseArea {
            width: 400
            height: 30
            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: 2;
        color: "red"

        ListView {
            id:listView
            height:500;
            model: comboBox.items
            currentIndex: 0;

            delegate: Item{
                width:comboBox.width;
                height: comboBox.height;
                Image {
                    id: img3;
                    source: modelData.img;
                    fillMode: Image.PreserveAspectFit
                    anchors.fill: parent
                }

                Text {
                    text: modelData.lang
                    anchors.top: parent.top;
                    anchors.left: parent.left;
                    anchors.margins: 5;
                    color: "#ffffff";

                }

                MouseArea {
                    anchors.fill: parent;
                    onClicked: {
                        comboBox.state = ""
                        chosenItemText.text = modelData.lang;
                        listView.currentIndex = index;
                        main_img.source = comboBox.items[index].img
                    }
                }
            }
        }
    }

    states: State {
        name: "dropDown";
        PropertyChanges { target: dropDown; height:60*comboBox.items.length }
    }

    transitions: Transition {
        NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 }
    }
}

效果如下:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值