有什么办法可以捕获qml中的模型更新信号。
这是我的示例程序。我有一个矩形,上面有 ListView 。
在鼠标上我正在更新列表模型。
代码:
Rectangle{
id: root
anchors.fill: parent
ListModel {
id: fruitModel
ListElement {
name: "Apple"
cost: 2.45
}
ListElement {
name: "Orange"
cost: 3.25
}
ListElement {
name: "Banana"
cost: 1.95
}
}
Component {
id: fruitDelegate
Row {
spacing: 10
Text { text: name }
Text { text: '$' + cost }
}
}
ListView {
id: list
anchors.fill: parent
model: fruitModel
delegate: fruitDelegate
onModelChanged: {
console.log("hi heloooo")
}
}
MouseArea{
anchors.fill: parent
onClicked: {
fruitModel.append({"cost": 5.95, "name":"Pizza"})//added new
fruitModel.remove(1) // deleted old. so count still same
}
}
}
单击鼠标时我正在更新模型,我只想在模型发生变化时捕获。
最佳答案
更改模型意味着什么?如果您对添加或删除的项目感兴趣,可以将监听器绑定(bind)到 ListView
的 onCountChanged
信号。
关于qt - 如何在ListView中捕获模型更新信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263913/