Can't access ListModel's roles
-
Hi! I'm still a beginner and trying to figure out why I get error "ReferenceError" model is not defined.
The Repeater documentation says: If the model is a model object (such as a ListModel) the delegate can access all model roles as named properties, in the same way that delegates do for view classes like ListView.
And here is my code I'm trying to do that but it doesn't work for some reason. It works if I access it like this: macroItemModel.get(index).macroCode but this sounds more complicated than what the documentation says.
(sorry for the pastebin it marked the post as spam with the code put it in here, between the code tags)
https://pastebin.com/grd7m3hA(Also there is another problem when I remove an element from ListModel I get 2 more TypeErrors: Cannot read property 'macroName', 'macroCode' of undefined)
-
This post is deleted!
-
The issue is that accessing roles in a delegate through context properties is not possible anymore when you define a required property.
If you change therequired property int index
inproperty int index: model.index
your delegate will work.
That's not the preferred solution however, you should use required properties for everything.So keep your required index, and make
macroName
andmacroCode
required. You can make them required in your delegate even if they are already defined inMacroItem
by writingrequired <propertyName>
:delegate: MacroItem { required property int index containerWidth: macroListContainer.width/1.5 required macroName required macroCode onDeleteButtonClicked: { macroItemModel.remove(index, 1); } }
-
P pillexyz_z has marked this topic as solved on