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. Dynamically appending the inner list.

Dynamically appending the inner list.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 1.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.
  • N Offline
    N Offline
    natraj
    wrote on last edited by
    #1

    I couldnt find a way to insert/append values of a list which is inturn a component of another list.

    If we consider this sample code :

    ListModel {
    id: mainList
    Component.onCompleted: {
    for(var i = 0; i < 2; ++i) {
    append({
    "role0": qsTr("ABC"),
    "contents": [
    {"someRole0": qsTr("aqs"), "someRole1": 123},
    {"someRole0": qsTr("qwer"), "someRole1": 12378}
    ]
    })
    }
    }
    }

    Here, the elements of the inner list "contents" are being added explicitly. But how do i append/insert elements dynamically as it is being done for the mainList?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thesourcehim
      wrote on last edited by
      #2

      Address the list by reference to the object within the model

      var obj=mainList.get(index);
      obj.contents.push(something);
      mainList.set(index. obj);

      1 Reply Last reply
      0
      • N Offline
        N Offline
        natraj
        wrote on last edited by
        #3

        @thesourcehim , thanks for the quick reply !

        i simply tried doing this :

        obj.contents.push("dummy");

        I got the following error:
        TypeError: Property 'push' of object QQmlListModel(0x1f2fab0) is not a function

        What exactly can i push into the contents?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thesourcehim
          wrote on last edited by
          #4

          What is "contents"? I assumed it is a simple array, If it is another ListModel, use it's append/insert methods.

          N 1 Reply Last reply
          0
          • T thesourcehim

            What is "contents"? I assumed it is a simple array, If it is another ListModel, use it's append/insert methods.

            N Offline
            N Offline
            natraj
            wrote on last edited by natraj
            #5

            @thesourcehim it is another list model and how to insert/append values to this inner list ( ie. contents) using the insert/append methods itself is my concern.

            if i do something like this:

            contents.insert(0,{"name"=dummy})

            i get an error saying :
            ReferenceError: contents is not defined

            how to access this inner list and dynamically insert/append values to the same using the insert/append methods ?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thesourcehim
              wrote on last edited by
              #6

              did you mean obj.contents.insert ?

              1 Reply Last reply
              0

              • Login

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