[Qt-qml] Dynamically created items and properties
Jan Ekholm
jan.ekholm at smultron.net
Fri Aug 20 10:30:39 CEST 2010
On Aug 20, 2010, at 9:42 AM, <bea.lam at nokia.com> wrote:
> Hi Jan,
>
>>
>> The "unit" is still a C++ object that gets assigned as soon as the Image is
>> created. I still get warnings like these for every line that contains a
>> "unit". The properties do work, but something is clearly wrong here:
>>
>> file:///.../qml/Unit.qml:27: TypeError: Result of expression 'unit'
>> [undefined] is not an object.
>> file:///.../qml/Unit.qml:20: Unable to assign [undefined] to QObject* target
>> file:///.../qml/Unit.qml:16: TypeError: Result of expression 'unit'
>> [undefined] is not an object.
>> file:///.../qml/Unit.qml:15: TypeError: Result of expression 'unit'
>> [undefined] is not an object.
>> file:///.../qml/Unit.qml:14: TypeError: Result of expression 'unit'
>> [undefined] is not an object.
>
>
> Usually the bindings are not set until the property is set to a proper value. I'm not sure why this is not happening in your case - perhaps the errors are occurring when the unit is destroyed.
The destruction is not triggered there yet, it happens much later when the user does some
actions. The errors come immediately when the Unit is created. The unit does work and all the
bindings seem to get updated, but the initial stream of errors is a bit worrying to me.
> If this is the case you could use states to ensure the bindings are only set when the unit has a proper value, e.g.
>
> Image {
> id: img
> property variant unit
>
> // don't set x, y, source here
>
> Text { id: label }
>
> states: State {
> name: "hasUnit"; when: unit != undefined
> PropertyChanges { target: img; x: unit.x * 48 + ( unit.y % 2 ) * 24; y: unit.y * 36; source: unit.icon }
> }
> }
Ha! I tried something like this, but not exactly like that. Your version works perfectly and there are
no longer any error messages. It is a bit of a kludge, but I'll take a cleanish kludge over errors any
day.
Thank you for the tip!
--
Jan Ekholm
jan.ekholm at smultron.net
More information about the Qt-qml
mailing list