Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Copy ListModel or complete ListView Component



  • I want to "save" a model state within a ListView.

    I fact I have created a navigation tree where you can click on a folder and the content of the listview model will be updated with the content of this folder.

    So, how to backup e.g the parent folder?

    I try to push the list model on a stack with

    stack.push("backup.qml", {listModel: list.model})

    but it seems that it only stores the reference of the model. If the model changes also the "backup" on the stack will change. I need something like a deep copy for qml elements.


  • Moderators

    Hi @sk2212,
    Do you mean you want to copy the contents of a ListModel ?



  • Aye.......


  • Moderators

    @sk2212 Wouldn't iterate and access elements work in your case ? ListModel has methods that can be useful.



  • Hmmm...okay, I think it works now.

    I will create "backup.qml" dynamically with Qt.createComponent() and createObject(root, {listModel: ListModel}).

    After that I access the property listModel in the created component, iterate over the current listModel which should be backuped and append that items in the listModel of the new-created component.

    At the end I push the new-created component on stack to be able to backup more than one "state" of the model.