[pygtk] Another sort problem
Brad Schick
schickb at gmail.com
Thu Jul 19 13:24:17 WST 2007
In my ListStore custom sort function that sometime I was getting None
values after appending new rows to a model. Instead of resorting the
model after a complete row is inserted, it looks like ListStore is
resorting after each column of a new row is inserted.
Run the example below and click the Append button. You will see that
'sort_func' is called twice, the first time with a value of None in
the second column of the model. This can be guarded against, but that
pretty ugly (and most people would not know it was needed).
Maybe this is a gtk+ bug? I'm not on any gtk+ mailing lists yet, so
I'm starting here.
------------------------------------------------------
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
def sort_func( model, iter1, iter2 ):
# If the next line is commented, the iterators become corrupted
print "model len", len(model)
print model.get_value(iter1, 1), model.get_value(iter2, 1)
result = ord(model.get_value(iter1, 1)) - ord(model.get_value(iter2, 1))
return min( max(result, -1), 1 ) # keep results in range
def append_model( button = None ) :
print "\n+++ append"
model.append( ('z','z') )
print "--- append\n"
return True
model = gtk.ListStore( str, str )
model.set_sort_func( 1001, sort_func )
map( model.append, (('a','f'), ('g','e'), ('c','n'), ('d','p')) )
model.set_sort_column_id( 1001, gtk.SORT_ASCENDING )
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(0)
render = gtk.CellRendererText()
col1 = gtk.TreeViewColumn( 'col1', render )
col1.add_attribute( render, "text", 1 )
col1.set_sort_column_id(1001)
tree.append_column( col0 )
tree.append_column( col1 )
tree.set_headers_clickable( True )
window = gtk.Window( gtk.WINDOW_TOPLEVEL )
window.connect( "destroy", gtk.main_quit )
vbox = gtk.VBox( False, 0 )
window.add(vbox)
button = gtk.Button( "Append" )
button.connect( "clicked", append_model )
vbox.pack_start( button )
vbox.pack_start( tree )
window.show_all()
gtk.main()
More information about the pygtk
mailing list