How to reset model in Qt?
Asked Answered
K

2

16

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.

Katydid answered 7/2, 2013 at 16:58 Comment(1)
Worth to mention that there are other methods that help to update views. All depends on what changes are made. Model reset is useful when you're performing big canges in model. There is beginInsertRow, beginMoveRows, beginRemoveRows. All those methods also apply to columns. So if you're going to perform small changes don't use beginResetModel as it will rebuild whole model in view instead of applying small changes on view.Voidable
A
19

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

Ashantiashbaugh answered 7/2, 2013 at 17:10 Comment(1)
Thanks, that did it.Effie
M
3

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)

Medicate answered 7/2, 2013 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.