Bind a QML property on JS object through a function

  • Hi,

    I would like to bind a QML property to a JS object.
    The binding should test wether the JS object owns a property or not.

    Something like:
    property bool myAttributeExists : myJSObject.hasOwnProperty("myAttribute")

    Since this doesn't work since myAttributeExists value nerver changes when I had a "myAttribute" attribute on myJSObject. A workaround is to wrap the test inside a function that has a dependency on a qml property to triggger binding reevalution:

    property bool myAttributeExists : propertyExists("myAttribute")

    property bool __dummyTrigger

    function propertyExists(name) {
    var __ = __dummyTrigger;
    return myJSObject.hasOwnProperty(name)

    function addProperty(name, value) {
    myJSObject[name] = value;
    __dummyTrigger = !__dummyTrigger;

    function removeProperty(name) {
    delete myJSObject[name];
    __dummyTrigger = !__dummyTrigger;

    Since JS objects doesn't trigger binding reevaluation I had to introduce a dummy qml property dependency (__dummyTrigger) in my properyExists function and update its value whenever I add/remove an attribute on the myJSObject.
    Then every time the dummy property property gets updated (boolean toggling) every qml expression calling this function gets reevaluated.

    This solution works but is cumbersome and hacky since this it involves qml boilerplate code.

    So I'm wondering if there is a better way to do it ?



  • Just stumbled upon this myself. Any idea?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.