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



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



  • 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



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