PDA

View Full Version : How to update model when data are stored and modified outside the model?



hind
21st October 2013, 06:03
This is what I do:
Make a QVector object to store data. Then make a model which keeps a pointer to the vector. I do it this way to make modifying vector easier.
Now I can display the data in a view. But what should I do when the vector has been updated? New item will be added so I don't think dataChanged() would always help.
Thanks in advance.

wysota
21st October 2013, 07:00
In general you should call beginInsertRows() and endInsertRows(). But I would suggest to avoid a situation when the vector can be modified outside of the model. Instead add any methods you might need to the model class that will operate on the vector. In those methods make sure you emit signals the model expects to be emitted.

hind
21st October 2013, 09:30
In general you should call beginInsertRows() and endInsertRows(). But I would suggest to avoid a situation when the vector can be modified outside of the model. Instead add any methods you might need to the model class that will operate on the vector. In those methods make sure you emit signals the model expects to be emitted.

Thanks!
I used to consider a model as an interface instead of a wrapper. Now it seems storing data in the model or not won't matter because you can only read/write the data with the model anyway.

wysota
21st October 2013, 10:15
It is an interface. It doesn't matter if the data is inside the model or not, what matters is that all operations on the data are done through the model.

anda_skoa
21st October 2013, 21:24
I used to consider a model as an interface instead of a wrapper.anyway.

That is a valid assumption, actually the very goal of most models.

You only need to make sure the model knows about changes to the actual data, but that can be done in various ways.
For example the class modifying the data could emit signals, the data could be in a class that has signals, the modifying code could tell the model, or, as wysota already said, the model can the only interface to the data.

Cheers,
_