2
votes

So I have created a nested list in QML:

ListModel {
id: players
    ListElement {
        name: "Player 1"
        counters: [
            ListElement {
                name: "Life"
                count: 20
                edit: false
            } ,
            ListElement {
                name: "Poison"
                count: 0
                edit: false
            }
        ]
    }
    //etc...
}

And, I have two views that read data from this: a ListView that is given the first-level ListModel, and a GridView that reads from the counters second-level model. I have given the GridView the counters list through the attached property in the delegate, like so:

GridView {
    model: counters
}

Initially, it reads the data just fine. But my problem is that when the second-level model data is updated from outside the delegate, the GridView does not update with the new data. How do I get it to update?

When looking around, I have seen other people just create two separate models, but the problem is that I need the information in the nested list to be associated with the ListElement that contains it, so that really is not an option.

1

1 Answers

1
votes

It turns out that you have to catch the signal and change the property manually. It's a little brute-force (because it will run given any change in the model, not just changes from outside the delegate), but it works.

In order to do this, you have to add a Connections element to the view delegate to catch the signal.

GridView {
    id: view
    model: counters
    delegate: Item {
        property int count: count
        Connections {
            target: view
            onDataChanged: Item.count = count
        }
    }
}

It is important that the Connections element is a child of the delegate, because the view creates/removes delegates at its whim, and as such it is difficult to access the delegates created by the view.