[Qt-qml] Dynamically created items and properties

bea.lam at nokia.com bea.lam at nokia.com
Fri Aug 20 08:42:23 CEST 2010


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.

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 }
	}
}


regards,

Bea



More information about the Qt-qml mailing list