[pygtk] Re: Indexing ListStore with iterators
Brad Schick
schickb at gmail.com
Thu Jul 19 12:24:45 WST 2007
On 7/18/07, Brad Schick <schickb at gmail.com> wrote:
> While writing a custom sort function for a gtk.ListStore, I found that
> using the provided iterator to access the model doesn't work
> correctly....
I hope I'm not missing some stupid error in my code, but this looks
like a bug. In the example below, if you comment out or remove the
first print statement in "sort_func", everything works as expected.
But if you leave in the print statement and click the first column in
the treeview, the sort gets stuck in a loop due to what looks like
corrupted iterators.
It looks like model.get_path is corrupting the iterators.
model.get_string_from_iter does the same thing. I've tried this on
both linux and windows under pygtk 2.10.4 with gtk 2.10.11 and python
2.5.1.
-Brad
-------------------------------------------------------
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
def sort_func( model, iter1, iter2 ):
# When the next line is left in place, the iterators are corrupted
print model.get_path(iter1), model.get_path(iter2)
print model.get_value(iter1, 0), model.get_value(iter2, 0)
result = model.get_value(iter1, 0) - model.get_value(iter2, 0)
return min( max(result, -1), 1 ) # keep results in range
model = gtk.ListStore( int, int )
model.set_sort_func( 1000, sort_func )
tree = gtk.TreeView( model )
tree.set_reorderable( False )
render = gtk.CellRendererText()
col0 = gtk.TreeViewColumn( 'col0', render )
col0.add_attribute( render, "text", 0 )
col0.set_sort_column_id(1000)
render = gtk.CellRendererText()
col1 = gtk.TreeViewColumn( 'col1', render )
col1.add_attribute( render, "text", 1 )
col1.set_sort_column_id(1)
tree.append_column( col0 )
tree.append_column( col1 )
tree.set_headers_clickable( True )
model.append( (1,5) )
model.append( (8,3) )
model.append( (2,6) )
model.append( (3,8) )
window = gtk.Window( gtk.WINDOW_TOPLEVEL )
window.add( tree )
window.connect( "destroy", gtk.main_quit )
window.show_all()
gtk.main()
More information about the pygtk
mailing list