[SOLVED] Adding items into array in qml script



  • Hello,
    I have following code in my function in QML file:
    @
    var params = {
    "name1": value1,
    "name2": value2
    };
    @
    Now later in my code I would like to add some more items into this array, but how can I do it? I have been trying to do it by calling some push or append method like this:
    @params.append({"name3":value3})@
    But it doesn't work. Thanks for any advice!



  • The array appears to be created incorrectly. Try:
    @
    var params = [{"name": "value1"}, {"name": "value2"}];
    params.push({"name": "value3"});
    @



  • Thanks, that seems to work. Thing is that I need to use this variable as an argument to a createObject function when creating dynamic compomnent like this:
    @
    ...
    var component = Qt.createComponent("myComponent.qml");
    var params = {
    "name1": "value1",
    "name2": "value2"
    };
    if (something){
    params.append({"name2":"value3"}); ///this doesn't work
    }
    component.createObject(parent, params);
    ...
    @
    If I created params variable as you suggested, I am unable to pass it as argument to call of createObject



  • The argument to createObject() is not an array; it's an object.

    EG: this is an array:
    var myArray = [ "some", "array", "elements" ];

    EG: this is an object:
    var myObj = { "v1": 1, "v2": 2, "v3": 3 }

    What you have (params) is an object. To add more properties, just set them.

    EG:
    var myObj = { "v1": 1, "v2": 2, "v3": 3 }
    myObj.v4 = 4;



  • Wow, that works as I needed. I am a bit more clever now, thanks a lot for clearing that out for me!:)


Log in to reply
 

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