I've the following scenario:
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
}
}
}
I'm able to append data in myModel with a js function like this:
function appendItem(a, b)
{
myModel.append({"titulo": a, "id": b})
}
The question: how I access myModel2 to append some data?
I've already tried the following, without success:
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);
}
}
}
Can you guys help me? Thanks!
THE SOLUTION:
The resolution was based on @Maciek comment and @derM explanation. As I was using QJsonArray, I thought I could just loop and then access the myModel2 child component. Well, it's not that easy like that, as explained in the comments, the component can not be easily accessed. I have tried in many ways, both by loop in QML and in c ++ using findChild <>. In the end, it was much easier to just loop in QJSonArray and put it all together in a string, pass as an argument in myModel.append and then use the js split function:
Component {
id: recipeDelegate
Item {
...
...
TagComponnent
{
id: row
myString: someData
}
}
}
Inside 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
}
}
}
}
To append:
function appendItem(a)
{
myModel.append("myString": someData)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments