On Tuesday 27 July 2010 01:42:36 Martin Jones wrote:

// see previous mail
...

> The above is not a binding.  It is an assignment.  Bindings are created
> using the colon syntax.
> 
> One solution is to provide the Unit to the QML item and let it setup the
> bindings:
> 
> Unit.qml:
> 
> Image {
>     property variant unit
>     x: unit.x * 48 + ( unit.y % 2 ) * 24
>     y: unit.y*36
>     source: unit.icon
>     unit_id: unit.id
> }
> 
> Then you just need to assign the unit to the QML item and it will take care
> of its bindings.

I still don't understand how this can sometimes work and sometimes not. As I 
tend to take all warnings as serious errors that happen later, I'm quite 
worried about the loads of warnings I get from code like the above. Slightly 
simplified this is what I have now:

Image {
    function unitDestroyed() {
        dummyId.destroy();
    }

    property variant unit: null

    id:      dummyId
    x:       unit.x * 48 + ( unit.y % 2 ) * 24
    y:       unit.y * 36
    source:  unit.icon

    Connections {
        target: unit
        onUnitDestroyed: unitDestroyed();
    }

    Text {
        text: unit.name
    }
}

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.

My understanding would be that "unit" should not be used as long as it is null 
and only when it changes (gets set externally) should it be used in 
expressions. The code that creates the above item is basically (error handling 
removed):

    function createUnits () {
        var component = Qt.createComponent("Unit.qml");

        // the c++ unit
        var unit = .... // C++ instance, get from somewhere

        // "units" is an Item
        var item = component.createObject( units );
        if (item != null) {
            // assign the variant property
            item.unit = unit
        }
    }
  
I do not know how this should be made totally cleanly, as I'm 100% sure that 
the above warnings will lead to crashes or code that stops working when I 
don't have time to fix it. Being able to safely bind to C++ properties and 
dynamically create items in a robust and clean way is very important.

-- 
        Five exclamation marks, the sure sign of an insane mind.
                                              -- Terry Pratchett, Reaper Man
_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to