简体   繁体   English

当另一个 comboBox 的索引发生变化时,如何更改 QML combobox 的内容?

[英]How to change content of a QML combobox when the index of another comboBox changes?

Basically If I have a comboBox that has two values.基本上,如果我有一个具有两个值的 comboBox。 If I choose the first, the second comboBox content changes into ["60"].如果我选择第一个,第二个 comboBox 内容变为[“60”]。 If it's the second index, then the second comboBox's values changes back to ["20","30","60"] which is the originally initialised content of the comboBox.如果是第二个索引,那么第二个组合框的值会变回 ["20","30","60"],这是 comboBox 的初始初始化内容。

 ComboBox
                  {
                      id: keyTypeComboBox
                      anchors.centerIn: parent
                      height: parent.height*0.8
                      width: parent.width*0.9
                      model: ["choice 1", "choice 2"]

                      onCurrentIndexChanged: changeContent();
                  }





 function changeContent()
    {
        if(ComboBox1.currentIndex == 0){
            ComboBox 2 change to ---->["60"]
        }else{
            ComboBox 2 change to ----> ["20","30","60"]
        }
    }

You can simply do this by binding currentIndex property from the first one combobox with the model property in the second one combobox:您可以通过将第一个 combobox 中的currentIndex属性与第二个 combobox 中的model属性绑定来简单地做到这一点:

ComboBox {
    id: combo1
    model: ["choice 1", "choice 2"]
}

ComboBox {
    id: combo2
    model: combo1.currentIndex === 0 ? ["60"] : ["20","30","60"]
}

Function's usage is not necessary here.函数的用法在这里不是必须的。

Never mind, I realized what I was doing wrong.没关系,我意识到我做错了什么。

it was the syntax.这是语法。 It should be:它应该是:

if(ComboBox1.currentIndex == 0){
        ComboBox2.model = ["insert data"]
    }else{
        ComboBox2.model = ["insert other data"]
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM