2017-05-05 77 views
0

我有以下情形:訪問的ListView模型

ListModel { 
    id: myModel 
} 

ListView { 
    id: listView 
    anchors.fill: parent 
    model: myModel 
    delegate: recipeDelegate 
}  

Component { 
    id: recipeDelegate 

    Item { 
     ... 
     ... 

     property var modelTag: row.model 

     ListView 
     { 
      id: row 
      delegate: recipeDelegateTags     
      width: parent.width 
      height: contentItem.height 

      model: ListModel { 
       id: myModel2 
       } 
     } 
} 

Component { // I need to access this 

    id: recipeDelegateTags 

    Item { 
     id: recipeTags 
     width: parent.width 
     height: 35 

     Text { 
      id: ic 
      font.family: fontAwesome.name 
      color: cor 
      font.pixelSize: 16 
      text: checked_ ? "\uf046" : "\uf096" 
      width: 15 

      anchors.left: parent.left 
      anchors.leftMargin: 50 
      anchors.verticalCenter: parent.verticalCenter 
     } 

     MediumText 
     { 
      anchors.verticalCenter: ic.verticalCenter 
      text: titulo 
      color: cor 
      font.family: localFont.name 
      anchors.left: ic.right 
      anchors.leftMargin: 10 
     } 
    } 
} 

我能夠基於myModel用js函數像這樣的附加數據:

function appendItem(a, b) 
{ 
    myModel.append({"titulo": a, "id": b}) 
} 

的問題:我如何訪問myModel2來追加一些數據?

我已經試過以下,但沒有成功:

function adicionaTag(a, b, c, d) 
{ 
    for(var i = 0; i < myModel.count; i++) { 
     var elemCur = myModel.get(i); 
     if(a === elemCur.id) { 
     console.log("property", elemCur.modelTag); 
     } 
    } 
} 

你們能幫助我嗎?謝謝!

+0

您wan't訪問'myModel2'?你嘗試過'myModel2.append()'嗎? – derM

+0

@derM是的,我做到了。事實上,我曾嘗試: elemCur.modelTag.append(B,C,d) elemCur.myModel2.append(B,C,d) 並收到此錯誤:**類型錯誤:無法調用「追加'未定義** 我必須使用屬性才能訪問父模型中的項目,對嗎? –

+0

在實例化之前,您無法訪問模型。如果你想通過'id'訪問一個對象,'id'需要在範圍內。所以 - 如果你在組件中有一個'id',並且多次實例化這個組件,'id'不會再是一個標識符,如果它們都處於同一個範圍內的話。因此,當你在其他文件中聲明瞭Component時,Component中的對象的id就會隱藏到這個Component外面的對象中。 – derM

回答

0

THE SOLUTION:

該解決方案基於@Maciek評論和@derM解釋。正如我使用QJsonArray,我想我可以循環,然後訪問myModel2子組件。那麼,就像評論中所說的那樣,這不是那麼容易,組件不容易訪問。我嘗試了很多方式,通過在QML中循環和在C++中使用findChild <>。最後,在QJSonArray中循環並將它們放在一個字符串中,通過在myModel中作爲參數傳遞會更容易。追加然後用JS分割功能:

Component { 
    id: recipeDelegate 

    Item { 
    ... 
    ... 

     TagComponnent 
     { 
     id: row 
     myString: someData    
     } 
    } 
} 

內TagComponent:

Item {  
    property string myString 
    property variant stringList 

    onMyStringChanged: 
    { 
     stringList = myString.split(';') 
     var arrayLength = stringList.length 

     for (var i = 0; i < arrayLength; i++) { 
      appendItem(stringList[i], stringList[i+1]) 
      i++ 
    } 
    } 


    function adicionaTag(a, b) 
    { 
     myModel.append({"titulo": a, "cor": b}) 
    }   

    ListModel { 
     id: myModel 
    } 

    ListView { 
     ... 
     model: myModel 
     delegate: recipeDelegateTags 
    } 

Component { 
    id: recipeDelegateTags 

     Item { 
      ... 
      ... 

      MediumText 
      { 
       text: titulo 
       color: cor 
      } 
     } 
    } 
} 

要追加:

function appendItem(a) 
{ 
    myModel.append("myString": someData) 
} 
0

我測試過的例子,我有幾個結論:

function adicionaTag(a, b, c, d) 
{  
    for(var i = 0; i< myModel.count; i++) 
    { 
     var elemCur = myModel.get(i); 
     if(a === elemCur.id) 
     { 
     console.log("property", elemCur) 
     } 
    } 
} 

Component 
{ 
    id: recipeDelegate 

    Item 
    { 

     property var modelTag: row.model 
     Component.onCompleted: 
     { 
      console.log("Item onCompleted, ", this) 
     } 

     ListView 
     { 

      id: row 
      delegate: recipeDelegateTags 
      width: parent.width 
      height: contentItem.height 

      model: ListModel { 
       id: myModel2 
       } 
     } 
    } 
} 

兩個輸出中的對象是不一樣的。 Component.onCompleted打印QQuickItem,但我們的adicionaTag函數提供了QObject。所以我認爲get函數返回的對象只包含由append或setProperty方法給出的屬性,可能類似於「附加屬性」。

你有兩個選擇,現在

一種是使用ListView的itemAt方法來獲得引用,然後添加你的數據。

第二個是要處理給你的模型的方法,即。您可以從您傳遞的數據中追加myModel2。

function appendItem(a, b) 
{ 
    myModel.append({"titulo": a, "id": b, "someData" : 55}) 
} 

然後:

Component 
{ 
    id: recipeDelegate 

    Item 
    { 
     property var modelTag: row.model 
     Component.onCompleted: 
     { 
     console.log("Item onCompleted, ", this) 
     row.model.append({"anotherData" : someData}) 
     } 
     ListView 
     { 
      id: row 
      delegate: recipeDelegateTags 
      width: parent.width 
      height: contentItem.height 

      model: ListModel { 
      id: myModel2 
      } 
     } 
    } 
} 

現在你可以從recipeDelegateTags查看在 「someData」:

Item { 
    id: recipeTags 
    width: parent.width 
    height: 35 
    Component.onCompleted: 
    { 
     console.log("child component: anotherData: ", anotherData) 
    } 
    ... 
} 

編輯: 也有另一種,第三種方式:

function adicionaTag(a, b, c, d) 
{ 
    for(var i = 0; i < listView.children.length; i++) { 
     var elemCur =listView.children[i]; 
     console.log("property", elemCur); 
    } 
} 
+0

選項2解決了我的問題。我將用您的解決方案更新問題。謝謝! –