[SOLVED] Adding items into array in qml script
-
wrote on 20 Mar 2012, 10:22 last edited by
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! -
wrote on 20 Mar 2012, 23:02 last edited by
The array appears to be created incorrectly. Try:
@
var params = [{"name": "value1"}, {"name": "value2"}];
params.push({"name": "value3"});
@ -
wrote on 21 Mar 2012, 06:10 last edited by
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 -
wrote on 21 Mar 2012, 07:33 last edited by
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; -
wrote on 21 Mar 2012, 07:36 last edited by
Wow, that works as I needed. I am a bit more clever now, thanks a lot for clearing that out for me!:)
1/5