QML:console.log() 无法识别委托的索引 属性
QML: index property of the delegate is not recognized by console.log()
我的委托组件的索引 属性 在 console.log()
函数中使用时无法识别:
onClicked: {
identities.qml_del_account(index);
console.log(index);
}
/*Application output:*/
qrc:/Accounts2.qml:74: ReferenceError: index is not defined
第 74 行是这样的:
console.log(index);
为什么它在第一行有效但在第二行失败?这两行都位于同一个 javascript 函数中。
完整的 QML 代码是:
Identities {
id: identities
}
ListView {
id: list_identities
width: list_area.width
height: 100
model: identities
delegate: Rectangle {
id: identities_delegate
height: 40
width: parent.width
Text {
id: identities_item
height: parent.height
anchors.left: parent.left
width: 100
text: email
}
Image {
source: "qrc:/images/dots-menu.png"
id: toolbtn_img
anchors.right: parent.right
width: 24
height: 24
MouseArea {
width: parent.width
height: parent.height
onClicked: {
identities.qml_del_account(index);
console.log(index);
}
}
}
}
}
该模型是用 C++ 定义的,它包含函数 qml_del_account()
,它工作正常,我没有抱怨它。
我的猜测是:qml_del_account
删除当前委托的模型条目,该委托随后被删除,因此日志在不再存在的对象模型条目上下文中执行。
尝试颠倒日志的顺序并调用模型的函数。
一般来说,我还建议通过 model
访问器引用委托中的模型数据来提高可读性,例如model.index
我的委托组件的索引 属性 在 console.log()
函数中使用时无法识别:
onClicked: {
identities.qml_del_account(index);
console.log(index);
}
/*Application output:*/
qrc:/Accounts2.qml:74: ReferenceError: index is not defined
第 74 行是这样的:
console.log(index);
为什么它在第一行有效但在第二行失败?这两行都位于同一个 javascript 函数中。
完整的 QML 代码是:
Identities {
id: identities
}
ListView {
id: list_identities
width: list_area.width
height: 100
model: identities
delegate: Rectangle {
id: identities_delegate
height: 40
width: parent.width
Text {
id: identities_item
height: parent.height
anchors.left: parent.left
width: 100
text: email
}
Image {
source: "qrc:/images/dots-menu.png"
id: toolbtn_img
anchors.right: parent.right
width: 24
height: 24
MouseArea {
width: parent.width
height: parent.height
onClicked: {
identities.qml_del_account(index);
console.log(index);
}
}
}
}
}
该模型是用 C++ 定义的,它包含函数 qml_del_account()
,它工作正常,我没有抱怨它。
我的猜测是:qml_del_account
删除当前委托的模型条目,该委托随后被删除,因此日志在不再存在的对象模型条目上下文中执行。
尝试颠倒日志的顺序并调用模型的函数。
一般来说,我还建议通过 model
访问器引用委托中的模型数据来提高可读性,例如model.index