【问题标题】:QML QComboBox.find() can not find element by textQML QComboBox.find()无法按文本查找元素
【发布时间】:2014-03-11 15:09:27
【问题描述】:

在 QML 中,我有一个 QComboBox:

ComboBox {
    id: myCBox
    model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]
 }

当我尝试通过文本在 ComboBox 中查找元素时,它返回 -1。

用这个记录:

console.log(myCBox.find("5"))

它输出-1(表示未找到)。

QML QComboBox documentation

【问题讨论】:

  • 你什么时候打那条线?

标签: qt qml qt-quick qcombobox


【解决方案1】:

你应该检查一下,你什么时候调用myCBox.find,看看这段代码:

ComboBox {
    id: myCBox
    model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]

    Component.onCompleted: {
        console.log("After this line it should work fine.");
    }

    Item {
        Component.onCompleted: {
            console.log("myCBox is not completed:", myCBox.find("5"));
        }
    }
}

Component.onCompleted: {
    console.log("myCBox here must be completed:", myCBox.find("5"));
}

和输出:

myCBox is not completed: -1
After this line it should work fine.
myCBox must be completed: 6

创建时,Component 创建所有项目,然后将它们排列在树中。从最内部的对象开始,它将属性和绑定更新为最被覆盖的值,并调用附加方法Component.onCompleted

【讨论】:

  • 我能够避免在启动时这样做,但您的回答很有价值并回答了我的问题。
猜你喜欢
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多