[solved] JavaScript object as QML property



  • Has anybody managed to create and use a component property that's a JavaScript object? So far, I've got the following: In the component:

    @
    property var rec: ({}); // This seems to create an empty JavaScript object
    @

    At the point where I want to use the component:

    @
    rec: { // note the colon! otherwise this would be mistaken as a grouped property!
    blah: "something";
    blupp: 42;
    }
    Component.onCompleted: { console.log ("Component completed, rec=" + rec); }
    @

    Output from console.log:

    @
    Component completed, rec=42
    @

    Apparently, only the last key in rec survives...?!



  • Answering my own question again. :) I feel like the only developer in this forum...

    I've spotted a mistake in the code above: A JavaScript object initializer should use commas to separate the values!

    @
    rec: { // note the colon!
    blah: "something", // note the comma!
    blupp: 42
    }
    @

    Unfortunately, this doesn't work either: now the QML parser complains...

    Is this a known restriction of the Qt JavaScript engine?


  • Moderators

    Hi

    @
    property var rec : { // note the colon!
    "blah": "something" , // note the comma! also note the double quotes :)
    "blupp": 42
    }
    @

    To access
    @
    rec.blah
    @



  • Aaah, now it works! Thanks a lot! :thumbsup:


Log in to reply
 

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