[Interest] QML property binding chains?

kai.koehne at nokia.com kai.koehne at nokia.com
Mon May 7 10:02:34 CEST 2012



> -----Original Message-----
> From: interest-bounces+kai.koehne=nokia.com at qt-project.org
> [mailto:interest-bounces+kai.koehne=nokia.com at qt-project.org] On Behalf
> Of ext Jeremy
> Sent: Friday, May 04, 2012 6:48 PM
> To: interest at qt-project.org
> Subject: [Interest] QML property binding chains?
> 
> Is there a way to configure a sort of cascading property evaluation/binding ?
> 
> For example, think about an RPG character, where one of his stats might be
> made up of an accumulation of an arbitrary number of values from other
> properties, even in other objects. For example, the characters 'defense' stat,
> might be made up of a base defense stat, calculated by the abilities of the
> character, and then modified by any number of items he has equipped. Is
> there a way to utilize the property binding of QML to help maintain accurate
> calculations of these stats between dependencies, as items are equipped,
> spells temporarily buff stats, etc

Huh, I'm not sure (ab)using QML bindings for this kind of logic is a good idea. They do not come for free, and are in general evaluated much more often one would naively think ... The general advice is to keep QML for the presentation , and do the actual logic in C++.

> From the data here http://doc.qt.nokia.com/4.7-
> snapshot/propertybinding.html, it looks like one option could be to bind these
> stats to functions that may calculate them arbitrarily, but it doesn't seem to go
> into much detail about how the binding works in a situation where you bind a
> property to a function. Under what conditions will the property bound
> function be automatically re-evaluated ? What if the function loops over other
> components or child components to accumulate information? Seems unlikely
> that a property binding would occur then, and even if it did, what happens
> when the values change?

The engine tracks which other properties are used in the function, and updates accordingly. E.g. in

import QtQuick 1.1

Rectangle {
    width: 360
    height: 360
    property int gg : width + height
    property int ff : f()

    onGgChanged: {
        console.log("gg: " + gg)
    }

    onFfChanged: {
        console.log("ff: " + ff)
    }

    function f() {
        return width + height
    }
}


You'll see that both gg and ff are updated a couple of times on startup. 

Regards

Kai



More information about the Interest mailing list