访问listView模型在另一个模型中

时间:2017-05-05 11:06:55

标签: javascript listview qml

我有以下情况:

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
        }
    }
}

我可以使用像这样的js函数在myModel中追加数据:

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);
      }
   }
}
你能帮助我吗?谢谢!

2 个答案:

答案 0 :(得分: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方法给出的属性,可能类似于&#34;附加属性&#34;。

您现在有两个选择

一种方法是使用listview的itemAt方法获取引用,然后附加数据。

第二个是研究给出模型方法的属性,即。您可以根据传递的数据附加myModel。

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
          }
        }
    }
}

现在您可以访问&#34; someData&#34;来自recipeDelegateTags:

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);
   }
}

答案 1 :(得分:0)

解决方案:

该决议基于@Maciek评论和@derM解释。当我使用QJsonArray时,我想我可以循环然后访问myModel2子组件。嗯,这并不是那么容易,正如评论中所解释的那样,组件无法轻易访问。我已经尝试过多种方式,包括QML中的循环和使用findChild&lt;&gt;的c ++中的循环。最后,在QJSonArray中循环并将它们放在一个字符串中更容易,在myModel.append中作为参数传递然后使用js split函数:

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)
}