Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Bind a QML property on JS object through a function
Forum Updated to NodeBB v4.3 + New Features

Bind a QML property on JS object through a function

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    X-Krys
    wrote on last edited by
    #1

    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 ?

    Thanks,

    X-Krys

    1 Reply Last reply
    0
    • S Offline
      S Offline
      supaiku
      wrote on last edited by
      #2

      Just stumbled upon this myself. Any idea?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved