gpt4 book ai didi

Qt QML 下拉列表就像在 HTML 中一样

转载 作者:行者123 更新时间:2023-12-04 09:19:40 28 4
gpt4 key购买 nike

简单的东西,却找不到。我想要一个带有几个选择的简单下拉选择框。就像在 HTML 中一样

<select>
<option>1</option>
<option>2</option>
</select>

QML 的代码是什么?

最佳答案

这是一个可以用作起点的简单示例:

import QtQuick 1.0

Rectangle {
width:400;
height: 400;

Rectangle {
id:comboBox
property variant items: ["Item 1", "Item 2", "Item 3"]
property alias selectedItem: chosenItemText.text;
property alias selectedIndex: listView.currentIndex;
signal comboClicked;
width: 100;
height: 30;
z: 100;
smooth:true;

Rectangle {
id:chosenItem
radius:4;
width:parent.width;
height:comboBox.height;
color: "lightsteelblue"
smooth:true;
Text {
anchors.top: parent.top;
anchors.left: parent.left;
anchors.margins: 8;
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:4;
anchors.top: chosenItem.bottom;
anchors.margins: 2;
color: "lightgray"

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

Text {
text: modelData
anchors.top: parent.top;
anchors.left: parent.left;
anchors.margins: 5;

}
MouseArea {
anchors.fill: parent;
onClicked: {
comboBox.state = ""
var prevSelection = chosenItemText.text
chosenItemText.text = modelData
if(chosenItemText.text != prevSelection){
comboBox.comboClicked();
}
listView.currentIndex = index;
}
}
}
}
}

Component {
id: highlight
Rectangle {
width:comboBox.width;
height:comboBox.height;
color: "red";
radius: 4
}
}

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

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

关于Qt QML 下拉列表就像在 HTML 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9634897/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com