PDA

View Full Version : Deleteting/Inserting rows in QAbstractItemModel



lightburst
7th April 2012, 14:33
I have a tree view that displays data from an external source which I have enclosed in a class. This data is dynamic in nature. I also subclassed a QAbstractItemModel and I am now able to display stuff initially with no problem. Having the dynamic nature of the data, I'm having trouble with how I would be able to update the QTreeView display without reloading everything. I read the documentation and some of the code samples but I find that implementing insertRows() and the other stuff somewhat obscure and have sought to use other means instead since I've read I don't have to implement them in order to achieve what I want.

insertRow()/removeRow() is implemented by QAbstractItemModel and is not virtual, whereas insertRows() is. I want to use that instead since I find it more straightforward. How would the QAbstractItemModel remove a 'row' in my model class if it has no idea how I implemented it? I imagine this to be possible if there were functions that deleted something at all by me but there isn't. Also, assuming that this would work, how would then I remove a row in the view? Do I have to manually call the functions of the view to delete a displayed row? Or do they sort of sync?

What are the other means to achieve this dynamic tree view? How would I use insertRow() in a subclassed abstract model? (note: no 's')

ChrisW67
9th April 2012, 01:22
QAbtractItemModel::removeRow() is a wrapper around the more general, and virtual, removeRows(). If there is a functional removeRows() in your subclass then insertRow() will function because the virtual function will be called. The same logic holds for insertRow()/insertRows() and the column equivalents.

If your removeRows() implementation correctly emits the beginRemoveRows() and endRemoveRows() signals then attached views will correctly update.

The whole exercise is covered in much detail in the Model subclassing reference as part of the broader picture.