required properties and delegates
-
Having recently upgraded to 5.15 I was working on an existing delegate and decided to tighten up the implementation by adding
required
to some of the custom properties in the delegate component. In the one and only place that the delegate component is used, these properties were already being assigned so this was a protection against future changes more than anything else. I did not consider this to be a significant change and I made it alongside various other changes that I believed to be more significant.MyDelegate.qml:
Item { required property real labelHeight required property ... required property ... ... }
MyListView.qml:
ListView { ... delegate: MyDelegate { labelHeight: 30 ... other required properties also set ... } }
When I ran my code the list view that the delegate relates to was showing as empty. On the console I saw lots of messages that suggested that the named roles from my model were not defined:
"ReferenceError: <role name> is not defined"
.After spending some time gradually backing out my changes, I finally alighted on it being caused by the
required
constraints I had added to the delegate. I only needed one such constraint to cause the issue. It didn't seem to matter which of the properties it was applied to.Are there some weird rules about when
required
should be used? Is it not appropriate in delegates for some reason?Edit: I guess the thing I found really odd about this is that I did not see any errors about the required properties, but it seemed to stop my model from being accessed in the delegate!
-
Having recently upgraded to 5.15 I was working on an existing delegate and decided to tighten up the implementation by adding
required
to some of the custom properties in the delegate component. In the one and only place that the delegate component is used, these properties were already being assigned so this was a protection against future changes more than anything else. I did not consider this to be a significant change and I made it alongside various other changes that I believed to be more significant.MyDelegate.qml:
Item { required property real labelHeight required property ... required property ... ... }
MyListView.qml:
ListView { ... delegate: MyDelegate { labelHeight: 30 ... other required properties also set ... } }
When I ran my code the list view that the delegate relates to was showing as empty. On the console I saw lots of messages that suggested that the named roles from my model were not defined:
"ReferenceError: <role name> is not defined"
.After spending some time gradually backing out my changes, I finally alighted on it being caused by the
required
constraints I had added to the delegate. I only needed one such constraint to cause the issue. It didn't seem to matter which of the properties it was applied to.Are there some weird rules about when
required
should be used? Is it not appropriate in delegates for some reason?Edit: I guess the thing I found really odd about this is that I did not see any errors about the required properties, but it seemed to stop my model from being accessed in the delegate!
@Bob64
I'm not sure if there is a written explanation for this.
But yes, you are right. If you use "required" on one property (doesn't matter which one). You will need to set "required" to all properties that are coming from your model in order to access them.Really weird behavior, but that's how it is on Qt5 and Qt6.
-
@Bob64
I'm not sure if there is a written explanation for this.
But yes, you are right. If you use "required" on one property (doesn't matter which one). You will need to set "required" to all properties that are coming from your model in order to access them.Really weird behavior, but that's how it is on Qt5 and Qt6.
@Marko-Stanke thanks - it sounds like there are indeed some "rules" around this. In my case, the required properties weren't even coming from the model, they were justparametrizing some aspects of the delegate.