Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 674
Dan O Avatar asked Sep 27 '09 00:09

Dan O


People also ask

How do you delete a list in Qt?

foreach (Type* t, list) delete t; list. clear(); c++

How do you delete a table in Qt?

About the content of table, you can use QTableWidget::clear to clear. But the table header, you should reset or delete them. you can use takeHorizontalHeaderItem(int column) to take header item, and then to delete it.


2 Answers

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.

like image 80
larsmoa Avatar answered Sep 17 '22 07:09

larsmoa


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.

like image 31
Caleb Huitt - cjhuitt Avatar answered Sep 19 '22 07:09

Caleb Huitt - cjhuitt