PDA

View Full Version : Editable Models



MTK358
9th September 2010, 20:43
It says that editable models should implement methods like insertRows(), etc.. But what if items can only be appended to the internal data structure and not inserted?

Can I implement custom functions to add items and not implement the normal ones, ir is there some other workaround?

wysota
9th September 2010, 20:58
But what if items can only be appended to the internal data structure and not inserted?
Appending is equivalent to inserting at the end. You can deny insertion that is not at the end of the model, if you want by returning false from QAbstractItemModel::insertRows().


Can I implement custom functions to add items and not implement the normal ones
Yes. Just remember to call beginInsertRows() and endInsertRows(). By not reimplementing the regular methods you will prevent views and proxy model from inserting data into your model.

In general I would advise to reimplement insertRows() but you can also implement your own method (for convinience) that would call insertRows() to do its job. By "behaving well" (i.e. keeping the standards) you make your classes more versatile and reusable which is a good thing in general.