简体   繁体   English

QML - 如何记住 combobox 选择

[英]QML - how to remember combobox selection

I have a problem with remembering my combobox selection.我无法记住我的 combobox 选择。

This is my code:这是我的代码:

App {
    id: app
    property var selectedColor: "green"

    NavigationStack {
        Page {
            id: page
            navigationBarHidden: true
            BackgroundImage { source: "../assets/background/default_" + app.selectedColor + ".png" }
            Text { text: qsTr("1st page") }
            Button {
                y: 30
                text: "go to 2nd page"
                onClicked: page.navigationStack.push(secondpage)
            }
        }
    }

    Component {
        id: secondpage
        Page {
          navigationBarHidden: true
          BackgroundImage { source: "../assets/background/default_" + app.selectedColor + ".png" }
          Text { text: qsTr("2nd page") }
          Button {
              y: 30
              text: "go to color selection"
              onClicked: page.navigationStack.push(chooseColorPage)
          }
        }
    }
    
    Component {
        id: chooseColorPage
        Page {
          navigationBarHidden: true

          ComboBox {
              id: colorComboBox
              editable: false
              activeFocusOnPress: true
              model: ListModel {
                  ListElement { text: "green" }
                  ListElement { text: "blue" }
                  ListElement { text: "orange" }
              }

              onCurrentTextChanged: app.selectedColor = currentText

              style: ComboBoxStyle {
                  label: Text {
                      text: control.currentText
                  }
              }
          }
          Button {
              y: 30
              text: "go to 1st page"
              onClicked: page.navigationStack.push(page)
          }
        }
    }
}

When I select a color in "chooseColorPage", the background image in the other pages changes as it should.当我 select 在“chooseColorPage”中使用颜色时,其他页面中的背景图像会发生应有的变化。 But once I open the "chooseColorPage" again, the label always says "green".但是一旦我再次打开“chooseColorPage”,label 总是显示“绿色”。 Even if I previously selected eg "blue".即使我之前选择了例如“蓝色”。 What do I need to change so that the selected color doesnt chage until the user clicks on another color in the combobox?在用户单击 combobox 中的另一种颜色之前,我需要更改什么以使所选颜色不会改变?

Any help would be appreciated!任何帮助,将不胜感激!

Your components are created and destroyed dynamically.您的组件是动态创建和销毁的。 Therefore, when you go back into "chooseColorPage" you are recreating the ComboBox.因此,当您将 go 返回“chooseColorPage”时,您正在重新创建 ComboBox。 Without code to load the current value of selectedColor into it, it will always default to the first item in the model - in this case "green".如果没有将 selectedColor 的当前值加载到其中的代码,它将始终默认为 model 中的第一项 - 在本例中为“绿色”。

To fix it, you need to create a function that fires when chooseColorPage becomes active.要修复它,您需要创建一个 function ,当 chooseColorPage 变为活动状态时触发。 Likely Component.onCompleted attached to the ComboBox would be good enough.可能 Component.onCompleted 附加到 ComboBox 就足够了。 In that function, you'll need to find the index of the current value of "selectedColor" in the ListModel and then set colorComboBox.currentIndex to it.在该 function 中,您需要在 ListModel 中找到“selectedColor”当前值的索引,然后将 colorComboBox.currentIndex 设置为它。

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

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