如何解决在新添加的listview条目中接收信号
我正在尝试在新添加到ListView的条目中执行功能。 通过按钮添加条目后,我要执行刚刚添加的委托的onNewEntry函数。但是只有旧的代表才能执行它。
最小示例:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.0
Window {
id:root
width: 640
height: 480
visible: true
signal newEntry(int new_row)
property var someProperty
ListModel {
id:listmodel
ListElement {
name: "Bill Smith"
}
ListElement {
name: "John Brown"
}
ListElement {
name: "Sam Wise"
}
}
ListView{
id: listView
width:100
height:200
model: listmodel
delegate: ItemDelegate{
id:delegateId
Text{
text:name
}
Connections { //ISSUE!!!: the new delegate doesnt execute this,just the old ones
target: root
function onNewEntry(new_row){
console.debug(index)
console.debug(new_row)
if(new_row==index){ //doesnt get true
listView.currentIndex = index
setProductData()
}
function setProductData(){
root.someProperty=name
}
}
}
}
}
Button {
anchors.top: listView.bottom
id: btnAdd
text:"+"
onClicked:{
listmodel.append({"name":"Joe Black"})
newEntry(listView.count-1) //emit signal newEntry
}
}
}
输出: qml:0 毫升:3 毫升:1 qml:3 毫升:2 qml:3
我已经提到的解决方法是在委托中使用ListView.onAdd:
delegate: ItemDelegate{
ListView.onAdd: {
setProductData()
}
我的问题是:为什么新添加的条目不收听newEntry信号。谢谢
解决方法
我找到了一个更好,可能更有效的解决方案:
delegate: ItemDelegate{
ListView.onAdd: {
listView.currentIndex=index
setProductData()
}
像以前的方法一样,仅新添加的委托不会全部接收onAdd。
,插入新行后设置currentIndex
会更容易:
Button {
id: btnAdd
onClicked: {
sqlTableModel.insertNewEmptyRow()
listView.currentIndex = listView.count - 1
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。