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. Adding values to a variant holding map of key-value pairs
Forum Updated to NodeBB v4.3 + New Features

Adding values to a variant holding map of key-value pairs

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.5k 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.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    I want to use the variant basic type http://doc.qt.nokia.com/4.7-snapshot/qml-variant.html in a dynamic way.
    Meaning, rather than initializing it in the declaration

    @
    property variant attributes: { 'color': 'red', 'width': 100 }
    @

    I want to be able to initialize it with a string I split and parse later on.

    Something like:

    @attributes.add('opacity' : 1.0);@

    What's the syntax for this?

    [EDIT: fixed link, code formatting, Volker]

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      Hi,

      You could possibly do something like this:

      @
      QtObject {
      property variant myobj

      Component.onCompleted: {
          var myjsobj = {};
          myjsobj.blah = 6;
          myobj = myjsobj;
          console.log(myobj.blah)
      }
      

      }
      @

      As shown above, for QtQuick 1.x you'll probably need to use a JS var to dynamically create the object map, and then assign it to your variant property.

      Alternatively, you could have a look at QDeclarativePropertyMap and see if it fits your needs.

      Regards,
      Michael

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #3

        Thanks. In this case I think I'm ok with just iterating through my list (it's not the classical property map usage) BUT I will definitely remember the QDeclarativePropertyMap for another time.

        "Roads? Where we're going, we don't need roads."

        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