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

  • I want to use the variant basic type 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]

  • Hi,

    You could possibly do something like this:

    QtObject {
    property variant myobj

    Component.onCompleted: {
        var myjsobj = {};
        myjsobj.blah = 6;
        myobj = myjsobj;


    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.


  • 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.

Log in to reply

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