PDA

View Full Version : How to delete List of child Nodes from QAbstractItemModel



chithara
13th May 2019, 07:52
Hi,

I have a QTreeView with Custom Read-only Model derived from QAbstractItemModel. (Followed Qt example:SimpleTreeModel).
During refresh call, I want to clear/delete all the child nodes under the selected parent node index. What should be the best way to achieve this ?

1. Do i need to call removeRows() on the selected index ? I don't think this will help as the items to be deleted are not coming from the view.

2. Do i need to loop through the childCount() and delete each ?

Or any other way to do this ?

ChrisW67
13th May 2019, 12:19
I have a QTreeView with Custom Read-only Model derived from QAbstractItemModel. (Followed Qt example:SimpleTreeModel).



1. Do i need to call removeRows() on the selected index ? I don't think this will help as the items to be deleted are not coming from the view.
2. Do i need to loop through the childCount() and delete each ?

If you have a read-only model then you cannot call removeRows() or removeRow(); these public methods modify the model content from outside.



During refresh call, I want to clear/delete all the child nodes under the selected parent node index. What should be the best way to achieve this ?

What is being refreshed? If the data underlying your read-only model (i.e. the private data that is being presented) is changing then you need to make matching beginInsertRows()/endInsertRows() or beginRemoveRows()/endRemoverows() calls in your model's code so that views using the model receive appropriate signals.

chithara
14th May 2019, 05:48
The private data (QList<object>) should get deleted and new QList should get added to the model.
I am not using beginInsertRows()/endInsertRows() for the first time to load the data to the model. But do i need to use these functions when the model data gets updated ?