2016-11-19 71 views
0

当在console.log()函数用于我的委托组件的索引属性不被识别:QML:委托的索引属性不被认可的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()的正常工作,我不是在抱怨。

回答

1

我的猜测是:qml_del_account删除当前委托的模型条目,随后将其删除,以便日志在不再存在的对象模型条目上下文上执行。

尝试颠倒日志的顺序并调用模型的功能。一般来说,我还建议通过参考model访问者的代表中的模型数据来提高可读性。 model.index

+0

你的猜测是完全正确的。非常感谢! – Nulik