tags:

views:

127

answers:

1

Hello. I've got a question about notifying a model of the changes made to some other object which it stores a reference to.

I'm aware of the signal dataChanged(), but I'm not sure how to use it. For example, my model stores a reference to some other object (let's call it myObjPtr). The model queries myObjPtr for its contents only when the methods rowCount(), columnCount(), data() and others are called on the model.

Then, if I make some changes to the contents of myObjPtr, how should I notify the model so that it could emit the dataChanged() signal with proper arguments? Should I use the model's setData() method? Which arguments should I pass to it if I, for instances, deleted or replaced some elements in myObjPtr?

Thank you.

+2  A: 

If I understand what you're asking, then the easiest way is for myObjPtr to have a signal it emits when it is changed, and have that emit the dataChanged() signal in the model. Bear in mind that the dataChanged() signal is specific about what data changed. If you can't get that degree of precision, you can also call reset() in the model, which causes all information to be updated.

Caleb Huitt - cjhuitt
Thanks for the hint on reset() method.Could you please explain the case with dataChanged() in more detail? I have asked in the original post about which arguments should be passed to dataChanged() when I delete/add/replace elements in myObjPtr (myObjPtr actually has a tree-like structure). Should I construct new instances of QModelIndex for such elements to be used in the call 'emit dataChanged(topLeft, bottomRight);'?
Alex
If myObjPtr can give enough information about what has changed, then use the dataChanged signal. Yes, you should create new instances of QModelIndex to do so. Be sure to either create them with the model's methods to do so, or to set the parent for the indexes properly, so that Qt knows exactly what model items have changed.
Caleb Huitt - cjhuitt