views:

207

answers:

3

I'm a little 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

A: 

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.

Caleb Huitt - cjhuitt
+1  A: 

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.

larsm
A: 

I don't know which way it truly "better" but removing all the rows can be a rather simple function something like:

void MyModel::Clear(void)
{
    // remove all data from internal data structures
    ...

    // Call QAbstractItemModel::reset to ensure any views know that everything has changed.
    reset();
}
TheUndeadFish