[Solved] QML ListView delete element from Model
I have a QML ListView, the data model is a QList<QVariant> from C++. The data inside the QVariant is a custom class. The list changes from time to time, how can I update the ListView?
The list can be changed from the C++ code (add or remove data) or by the user from the QML Application. For example, the user can select a ListView entry and click on a delete button. Now I have the problem I don't know how to delete the selected element from QML.
I tried to invoke a C++ method which deletes the element from the QList, this works, but the QML ListView does not recognize the change of the list.
So I connected the destroyed() signal of the data class in the QList to a slot of the ListView delegate, here I called destroy(), but I got the message "Error: Invalid attempt to destroy() an indestructible object"
How do I delete a object from the QML ListView?
You need to either reset the model after every change, or use QListItemModel.
Thanks, reset worked, but I'm not happy with this solution (could be a performance killer). QListItemModel does not exist (or am I blind?), I think you mean I have to subclass QAbstractItemModel.
Yes, if you have the time to implement a proper model, it is a better and more performant solution than the "reset" approach.
Sorry, my bad, I've given a wrong name from memory. Here is the list model: "link":http://doc.qt.io/qt-5/qabstractlistmodel.html. It is just a minimal extension for QAbstractItemModel - you can use either of those, but since we are talking about List View, I suggested a list model :-)
Thank you, I set the title to solved
Great, you are welcome, and happy coding :-)