[Qt-qml] Property Bindings *within* Javascript?

Charley Bay charleyb123 at gmail.com
Sat Oct 23 16:49:27 CEST 2010


The QML docs state:

> Property bindings are the most common use of JavaScript in QML.
> Property bindings associate the result of a JavaScript expression
> with a property of an object.

...found at:

<http://doc.qt.nokia.com/4.7/qdeclarativescope.html>

IMHO, this is one of the most pivotal concepts in QML.

However, I'm trying to figure out how to *set* such a binding
*within* javascript.  Can this be done?

For example, when an Item is re-parented, I want to
bind a property from the new parent to a property in
that child that is being re-parented:

Item {
  id: myParent
  property int myParentProperty: 42
  //...
  onChildrenChanged: {
    for(var i = 0; i < myParent.children.length; ++i) {
      // THIS WILL SET, BUT NOT BIND, THE PROPERTIES.
      myParent.children[i].myChildProperty = myParentProperty
    }
}

I'm interested in "binding" the parent property to the child
property because the parent property value actually
changes through an animation, and I want the child
property to stay "in sync".  (I suppose a work-around
would be to dynamically create an animation for the
child when the child is re-parented, where that animation
has properties bound to properties in the parent --?)

As I read the docs, the binding occurs *from the results*
of javascript across the ":" into QML, not within javascript.
Of course, inside the "onChildrenChanged", we only *have*
javascript.

Is there some kind of API for this?

GOAL:  Establish binding from parent property to child
property when a child is re-parented (I assume this
is best performed inside "onChildrenChanged()")

Thanks!

--charley



More information about the Qt-qml mailing list