Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Adding items into array in qml script
Forum Update on Monday, May 27th 2025

[SOLVED] Adding items into array in qml script

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 27.1k Views
  • 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.
  • P Offline
    P Offline
    portoist
    wrote on 20 Mar 2012, 10:22 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dajansen
      wrote on 20 Mar 2012, 23:02 last edited by
      #2

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

      QtQuick Quality Engineer / Lab Monkey
      Nokia Brisbane

      1 Reply Last reply
      0
      • P Offline
        P Offline
        portoist
        wrote on 21 Mar 2012, 06:10 last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chriadam
          wrote on 21 Mar 2012, 07:33 last edited by
          #4

          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;

          1 Reply Last reply
          0
          • P Offline
            P Offline
            portoist
            wrote on 21 Mar 2012, 07:36 last edited by
            #5

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

            1 Reply Last reply
            0

            5/5

            21 Mar 2012, 07:36

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved