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. How to Push multiple Values to an Array in a single time
Forum Update on Monday, May 27th 2025

How to Push multiple Values to an Array in a single time

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 394 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
    Praveen.Illa
    wrote on 10 Feb 2022, 07:27 last edited by
    #1

    Hi Team,

    I am having an issue to Push multiple Values to an Array in a single time.
    I am not good in QML programming.
    Can someone please help me on this.

    The ouput from the below code is
    qml: Push = [object Object],[object Object]
    qml: Pop = [object Object]

    Please find below sample code

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        property var list: []
    
        Component.onCompleted: {
    
    	list.push({"A001": "1"})
    	console.log("Push = " + list) //expecting output to be {"A001","1"}
    		
            list.push({"A002": "2"})
            console.log("Push = " + list) //expecting output to be {"A001","1"}, {"A002","2"}
    
            list.pop()
            console.log("Pop = " + list) //expecting output to be {"A001","1"}
        }
    }
    
    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemons
      wrote on 10 Feb 2022, 15:11 last edited by lemons 2 Oct 2022, 15:31
      #4

      if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list

      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          property variant list: []
          property string str: "A001"
          property int value: 1
      
          // option 2
          property var object: ({})
      
          Component.onCompleted: {
              // option 1 - do it in js only
              let obj = {}
              obj[str] = value
              list.push(obj)
              console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1}
      
              // option 2 - use property
              object[str] = value
              list.push(object)
              console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1}
      
              // note if value should be of type string:
              object[str] = value.toString()
              list.push(object)
              console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"}
          }
      }
      
      P 1 Reply Last reply 10 Feb 2022, 16:06
      0
      • L Offline
        L Offline
        lemons
        wrote on 10 Feb 2022, 09:41 last edited by
        #2

        As your list entries are JSON objects, you have to stringify them before logging.
        Try this instead:

        Component.onCompleted: {
            list.push({
                          "A001": "1"
                      })
            // note that you can simply separate the log outputs with commas
            // no need for string concatenation
            console.log("Push =", JSON.stringify(list))
        
            list.push({
                          "A002": "2"
                      })
            console.log("Push =", JSON.stringify(list))
        
            list.pop()
            console.log("Push =", JSON.stringify(list))
            
            // pushing multiple entries at once
            list.push({
                          "A002": "2"
                      }, {
                          "A003": "3"
                      }, {
                          "A004": "4"
                      }, {
                          "A005": "5"
                      })
            console.log("Push =", JSON.stringify(list))
        }
        
        P 1 Reply Last reply 10 Feb 2022, 12:37
        0
        • L lemons
          10 Feb 2022, 09:41

          As your list entries are JSON objects, you have to stringify them before logging.
          Try this instead:

          Component.onCompleted: {
              list.push({
                            "A001": "1"
                        })
              // note that you can simply separate the log outputs with commas
              // no need for string concatenation
              console.log("Push =", JSON.stringify(list))
          
              list.push({
                            "A002": "2"
                        })
              console.log("Push =", JSON.stringify(list))
          
              list.pop()
              console.log("Push =", JSON.stringify(list))
              
              // pushing multiple entries at once
              list.push({
                            "A002": "2"
                        }, {
                            "A003": "3"
                        }, {
                            "A004": "4"
                        }, {
                            "A005": "5"
                        })
              console.log("Push =", JSON.stringify(list))
          }
          
          P Offline
          P Offline
          Praveen.Illa
          wrote on 10 Feb 2022, 12:37 last edited by
          #3

          @lemons Thank you for your quick response and the solution provided.

          But, if I pass the variable names to the push method, the console output is different like below
          qml: [{"str":"A001","value":1}]

          I am expecting output to be {"A001","1"}
          How can I do this ?

          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              property variant list: []
              property string str: "A001"
              property int value: 1
          
              Component.onCompleted: {
          
                  list.push({str, value})
                  console.log(JSON.stringify(list))  //expecting output to be {"A001","1"}
              }
          }
          
          1 Reply Last reply
          0
          • L Offline
            L Offline
            lemons
            wrote on 10 Feb 2022, 15:11 last edited by lemons 2 Oct 2022, 15:31
            #4

            if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list

            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
            
                property variant list: []
                property string str: "A001"
                property int value: 1
            
                // option 2
                property var object: ({})
            
                Component.onCompleted: {
                    // option 1 - do it in js only
                    let obj = {}
                    obj[str] = value
                    list.push(obj)
                    console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1}
            
                    // option 2 - use property
                    object[str] = value
                    list.push(object)
                    console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1}
            
                    // note if value should be of type string:
                    object[str] = value.toString()
                    list.push(object)
                    console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"}
                }
            }
            
            P 1 Reply Last reply 10 Feb 2022, 16:06
            0
            • L lemons
              10 Feb 2022, 15:11

              if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list

              Window {
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
              
                  property variant list: []
                  property string str: "A001"
                  property int value: 1
              
                  // option 2
                  property var object: ({})
              
                  Component.onCompleted: {
                      // option 1 - do it in js only
                      let obj = {}
                      obj[str] = value
                      list.push(obj)
                      console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1}
              
                      // option 2 - use property
                      object[str] = value
                      list.push(object)
                      console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1}
              
                      // note if value should be of type string:
                      object[str] = value.toString()
                      list.push(object)
                      console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"}
                  }
              }
              
              P Offline
              P Offline
              Praveen.Illa
              wrote on 10 Feb 2022, 16:06 last edited by
              #5

              @lemons
              Thanks again and Sorry for troubling you again
              I understood, how to store the key value pair in the list using json object.
              But, one thing that is disturbing in my mind is, if I want to push 3 or 4 arguments to the list like below
              ({"A001", 1, "Item1"}, {"A002", 2, "Item2"})
              How can I do this ? I know these are the basic things in js, but can't figure out the solution

              1 Reply Last reply
              0

              1/5

              10 Feb 2022, 07:27

              • Login

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