Can we append dynamic instantiated object(ListElement) to ListModel?



  • Is it possible to append dynamic instantiated object(ListElement) to ListModel?

    Dear all, I seek your kind help in the append function of ListModel
    I read in the example that we can append to ListModel by the append function, for example

    @
    mediaIconModel.append({"name": "Jim Williams", "portrait": "jim.jpg"});
    @

    Is it possible to append an instantiated object by this way?

    @
    var iconObj = Qt.createComponent("newIcon.qml");
    iconObj.createObject(parent);
    mediaIconModel.append(iconObj);
    @

    where newIcom.qml contains

    @
    import QtQuick 1.0

    Image {
    id: icon
    width: 64
    height: 64
    source: "koala.jpg"
    property var name: "koala";
    property var portrait: "koala.jpg"
    }
    @

    But I have this error

    Unable to assign [undefined] to QString text
    Unable to assign [undefined] to QUrl source



  • If you look at ListModel documentation, all the elements of the ListModel should be of type ListElement. But what you are tying to append is off some other type.

    And you can not append displayable qml components to ListModel. Can you try using VisualItemModel rather than ListModel. You can have displayable items added to VisualItemModel and you don't need to provide deligate as your ListModel elements are displayable by themselves.



  • Vijay, thank you for your suggestion.but I need to append new object to the gridModel, I am trying out VisualItemModel, and it seems that it does not support the append function.



  • If it makes sense to your use case why don't you use a simple C++ model instead. In the delegate create the item of interest as you are doing earlier.



  • mm.. sorry did not realize that VisualListModel does not have append() method.

    If your goal is to add a new Image item to ListView dynamically, make Image component as deligate to the ListView and have a ListModel .

    And when ever you want to add a new Image to ListView, Just append() an element to listModel like

    @ listModel->append({"name" : "koala", "source" :"koala.jpg"}) @

    Don't know if this is what you are looking for.



  • Hi Vijay and srikanthsombhatla,

    Thank you for your suggestions, I will use them for now. The values can be used as variables, e.g.

    @
    var name = 'koala';
    listModel->append({"name" : name, "source" :"koala.jpg"});
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.