【发布时间】:2020-06-03 05:43:49
【问题描述】:
我想在 Qml 中设计带有复选框的 ComboBox(Dynamic)。 当我检查组合框中的复选框时,我想在组合框下的列表视图中添加一个元素。 请帮我解决这个问题
下面附上示例图片
ApplicationWindow {
id: applicationWindow
visible: true;
width: screen.width;
height: screen.height;
ComboBox {
id: comboBox
width: parent.width
height: parent.height/20
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: 40
anchors.rightMargin: 40
anchors.top: parent.top
anchors.topMargin: 10
onCurrentIndexChanged: {
var receivedData = {
imageSource: "bulb",
loadType: "Light",
loadName: "▶BedRoom1",
loadStatus: false,
macId: "00:17:F1:00:00:B8_01",
};
var receivedData1 = {
imageSource: "ac",
loadType: "AC",
loadName: "▶BedRoom21",
loadStatus: true,
macId: "00:17:F1:00:00:B8_02",
};
var receivedData2 = {
imageSource: "dimmer",
loadType: "Dimmer",
loadName: "▶Hall1",
loadStatus: false,
macId: "00:17:F1:00:00:B8_03",
};
var receivedData3 = {
imageSource: "fan",
loadType: "Fan",
loadName: "▶Hall1",
loadStatus: false,
macId: "00:17:F1:00:00:B8_04",
};
console.debug(combomodel.get(currentIndex).text)
if(combomodel.get(currentIndex).text === "Light")
{
loadListView(receivedData)
}
else if(combomodel.get(currentIndex).text === "Bulb")
{
loadListView(receivedData)
}
else if(combomodel.get(currentIndex).text === "Ac")
{
loadListView(receivedData1)
}
else if(combomodel.get(currentIndex).text === "Dimmer")
{
loadListView(receivedData2)
}
else if(combomodel.get(currentIndex).text === "Fan")
{
loadListView(receivedData3)
}
}
model: ListModel {
id:combomodel
ListElement {
//text:"Select Device"
name: ""
checked: false
}
}
function loadListView({imageSource,loadType,loadName,loadStatus,macId})
{
listmodel.append({"src": imageSource,"load":loadType,"label" : loadName,"checkedStatus" : loadStatus,"macStatus" :macId })
}
delegate: Item {
width: parent.width
implicitHeight: checkDelegate.implicitHeight
CheckDelegate {
id: checkDelegate
width: parent.width
text: model.name
highlighted: comboBox.highlightedIndex === index
checked: model.checked
onCheckedChanged:
{
model.checked = checked
}
}
}
}
function appendData()
{
combomodel.append({ "name":"S" } )
combomodel.append({ "name":"U" } )
combomodel.append({ "name":"R" } )
combomodel.append({ "name":"A" } )
combomodel.append({ "name":"J" } )
combomodel.append({ "name":"T" } )
}
Component.onCompleted: {
getModuleTableData()
}
ListView {
id:listview
anchors.top: comboBox.bottom
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: 5
anchors.rightMargin: 5
anchors.topMargin: 10
flickableDirection: Flickable.VerticalFlick
boundsBehavior: Flickable.StopAtBounds
clip: true
//delegate: deviceDelegate
model: ListModel{
id:listmodel
}
focus: true
ScrollBar.vertical: ScrollBar {}
}
Dbadaptor{
id:dbadaptor
}
function getModuleTableData()
{
console.log("In getModuleTableData() function")
var data = dbadaptor.fetchModuleTable()
console.log("Data: ",data)
console.log("length",data.length)
for(var i = 0; i< data.length;i++)
{
console.log("Name: ",data[i])
//if(data[i] == "")
combomodel.append({"name": '<b>'+data[i]+'</b>',"ckecked": ""})
var dataload = dbadaptor.fetchLoadDataForGroup(i+1)
console.log("loadData :",dataload)
console.log("loaddatalength: ",dataload.length)
for(var j = 0; j<dataload.length;j++)
{
combomodel.append({"name" : dataload[j],"checked": false})
}
}
}
}
在上面的代码中,我在带有复选框的 ComboBox 中附加数据,当我单击 CheckBox 时,我想打印一条调试消息。 在 8x1 设备名称所在的 ComboBox 中,我不想针对它附加复选框。
【问题讨论】:
-
如果您希望有人为您编写所有代码,那么您选择了错误的站点。请提供您解决问题的尝试,预期目标是什么以及结果是什么。显示具体错误、困难等。请阅读如何创建minimal reproducible example 和How to Ask。或许下面的文章可以帮到你:Models and Views in Qt Quick,QML Dynamic Objects
-
@folibis,我正在添加我的代码。我将更新我的查询
-
请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已在 CC BY-SA license 下授予 SE 分发内容的不可撤销的权利(无论您未来的选择如何)。根据 SE 政策,分发非破坏版本。因此,任何此类破坏性编辑都将被还原。请参阅How does deleting work?,了解有关如何在本网站上删除内容的更多信息。
标签: javascript qt qml qt5