Dynamically appending the inner list.



  • 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?



  • Address the list by reference to the object within the model

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



  • @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?



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



  • @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 ?



  • did you mean obj.contents.insert ?


Log in to reply