您的位置:首页 > 其它

QML ComboBox 图片加文字

2016-03-29 13:51 465 查看
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 }
}
}


效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: