2016-11-19 72 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