Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset model in Qt?

Tags:

qt

model-view

I am using Qt model/view framework. When I reset the data in the model, I have to reset the model to let views update also. How to do it? I found a signal modelReset(QPrivateSignal); in the QAbstractItemModel, Is this the way to solve it? How to emit the signal? Thanks.

like image 491
user1899020 Avatar asked Feb 07 '13 16:02

user1899020


People also ask

What is QAbstractItemModel?

The QAbstractItemModel class provides the abstract interface for item model classes. The QAbstractItemModel class defines the standard interface that item models must use to be able to interoperate with other components in the model/view architecture. It is not supposed to be instantiated directly.

What is a model class in Qt?

Models. All item models are based on the QAbstractItemModel class. This class defines an interface that is used by views and delegates to access data.

When implementing a table based model rowCount () should return 0 when the parent is valid?

From the official documentation: When implementing a table based model, rowCount() should return 0 when the parent is valid. The same goes for columnCount() . And for completeness, data() should return None if the parent is valid.


2 Answers

You call beginResetModel() before you reset your data, and then endResetModel() once you have finished. The endResetModel() emits the private signal.

like image 198
cmannett85 Avatar answered Oct 07 '22 08:10

cmannett85


As you can see here, the preferred method is to use the beginResetModel() and endResetModel() functions surrounding your reset code in your model subclass.

This should handle emitting the signal appropriately.

Some of the qabstractitemmodel subclasses might allow you to use model.removeRows(0,model.rowCount()) (others would require you to implement it yourself)

like image 38
jkerian Avatar answered Oct 07 '22 07:10

jkerian