[Interest] Design Issue with QAbstractItemModel & QTreeView

Sébastien Le Ray sebastien-qt at orniz.org
Fri Aug 5 09:05:27 CEST 2016


Le 05/08/2016 à 00:54, John Weeks a écrit :
>> But when childAdded is called… child already has been added. That's the issue, model is not manipulating data, it's just supposed to react on their changes
> OK, I finally decided to look at our code and the documentation :)
>
> QAbstractItemModel has protected functions beginInsertRows and endInsertRows, as well as a few other beginXxx and endXxx functions. When data is added to the underlying data set, you call beginInsertRows(), do your work of packaging up QModelIndexes and inserting them, then call endInsertRows(). Since your subclass of QAbstractItemModel is only a representation of the data, not the data itself, you don't care of the childAdded signal comes after a data item has been added. At that point you prepare a new row in your QAbstractItemModel subclass.

When you say "packaging", you mean that you have a wrapper around the 
underlying data (something like a TreeItem class that "caches" data)?
Main issue is not on adding rows (this you can indeed call begin while 
data is already added), issue is on removal since beginRemoveRows needs 
to access to the to-be-removed row, which is an issue if row has already 
been removed



More information about the Interest mailing list