Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    QML and Qt Quick
    2
    3
    3202
    Loading More Posts
    • 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
      frankiefrank last edited by

      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 Reply Quote 0
      • M
        mbrasser last edited by

        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 Reply Quote 0
        • F
          frankiefrank last edited by

          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 Reply Quote 0
          • First post
            Last post