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. [solved] JavaScript object as QML property

[solved] JavaScript object as QML property

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.9k Views
  • 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.
  • D Offline
    D Offline
    digorydoo
    wrote on last edited by
    #1

    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...?!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      digorydoo
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Hi

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

        To access
        @
        rec.blah
        @

        157

        1 Reply Last reply
        0
        • D Offline
          D Offline
          digorydoo
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Desenvolvimento Companytec
            wrote on last edited by
            #5
            QtObject {
                id: myData
                property bool variable1: false
                property String variable2: ""
            }
            
            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