Avoid looping through treeview iterators with row references

blue-chart-green-arrow-125x125Keeping a dictionary of treerowreferences is the way to go. It makes keeping track of rows a breeze. You don’t have to worry about row locations changing over time as the treerowref keeps track of that automatically.

When you add a parent row to a treestore you get an iterator returned. Use that to get the path of your newly added row and combine the path with the model to get the row reference. You only need to do this once.

x = 'track-me'
self.rowref = {}
titer = treestore.append(None, [x, 'Unknown', None, None, 0]
path = treestore.get_path(titer)
self.rowref[x] = gtk.TreeRowReference(treestore, path)

Never iterator through your models again to find a row.

it = treestore.get_iter_first()
while it:
    if x == treestore.get_value(it, 0):
        titer = it
    it = treestore.iter_next(it)

That loop can be replaced with this:

titer = treestore.get_iter(self.rowref[x].get_path())

However, keeping track of lots of row references takes its toll as well. Everytime you add something, the row references have to adjust themselves. So if you have really large models, try to delete references you won’t need anymore.

Posted by admica   @   27 September 2011

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

*

Previous Post
«
Next Post
»
Powered by Wordpress   |   Lunated designed by ZenVerse

Valid XHTML 1.0 Transitional