PDA

View Full Version : What is the right time to refresh tree view of underlying data structure



JPNaude
3rd October 2011, 11:23
Hi

I've been using a custom tree view model for a long time which shows data contained in an external data structure. Whenever the data structure itself changes I update the model's representation of the tree and call reset. When only data in the data structure changes I emit dataChanged().

This works well, but its not as fast as it possibly can be. I am trying to optimize this and the main performance issue is that whenever the data structure changes I rebuild the internal representation of the tree. This happens even if the view in which the model is used is not visible. Thus, its a waste. I'm now looking for a way to only rebuild the internal tree representation when the view is visible. Thus, if an update is required when the view is not visible, I delay it until the view becomes visible.

My question is, what is the right place to know that the view becomes visible. I thought of the following ways:
- Do it the first time the model receives a request from the view (seems to be in rowCount()).
- Subclass the view class and do it in the paint() function.
- Install an event filter on the view and do it when an appropriate event happens (focusIn perhaps).

Any ideas on what is the optimal way to do this will be appreciated.

Thanks,
Jaco

Added after 20 minutes:



- Do it the first time the model receives a request from the view (seems to be in rowCount()).


I realize now that rowCount() is const, thus it is probably not the place to do this.

Added after 47 minutes:

Ok, rowCount() implementation does the job for me.
Thanks,
Jaco

wysota
3rd October 2011, 16:40
The ideal approach would be to use the underlying data structure directly without any need to rebuild anything.