[Interest] Proper way of updating a model?

Bo Thorsen bo at fioniasoftware.dk
Wed Aug 22 21:44:48 CEST 2012

Den 22-08-2012 20:29, Stephen Chu skrev:
> Thanks to everyone who helped on the 64-bit internal ID question I
> posted. I end up maintaining a copy of index-and-id mapping to work
> around it.
> Now I have a question on how to properly update the model. My model is a
> read-only representation of a database. I am using a QTreeView to hold
> the model. The data themselves are not tree-like but I like the visual
> of QTreeView better.
> The model needs to update itself periodically from the remote database.
> I keep a QList of data IDs and rebuild the list every time I get a new
> snapshot from the database.
> What's the recommended way to reflect the changes to the view? I need to
> keep the current selection and scroll position of the QTreeView between
> updates.
> Do I reset the model? Or do I remove deleted rows and insert new ones?

If you reset the model, you will destroy selection and scroll info. 
Easy, but bad. Again :)

You have to do the beginDeleteRows(...) and beginInsertRows(...) stuff 
and emit dataChanged for existing rows that changed values.

Bo Thorsen,
Fionia Software.


Expert Qt and C++ developer for hire
Contact me if you need expert Qt help

More information about the Interest mailing list