Clearing/resetting a model in qt (removing all rows)


Question

I'm confused about what the correct way to reset or clear the data in associated with a QAbstractItemModel?.

I'm writing an application in which the user can "start over" with a new set of data (empty, or small).

Should I be deleting the old model when the user makes this request? Or should I leave the model alone and just remove all of the rows?

Regards, Dan O

1
9
8/22/2018 3:12:25 PM

Accepted Answer

Generally I would prefer to have the model react to changes and take the necessary actions to update it's view (indirectly ofcourse). However, programming models can be (=is) a PITA, so I would probably look through the fingers if I was reviewing code that created a new model and deleted the old one. Only do this if you are sure the user only will delete all rows. If the user may delete items from the model incrementally you're probably best off implementing removal properly in the first place...

Also, ModelTest might help you discover problems with your Qt models.

5
8/23/2018 10:50:04 AM

If the user is truly starting over with a new set of data, then it makes sense to me to simply delete the old model and create a new one. Simple, effective, and it matches up to what the user is doing.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon