[pygtk] filtered and sortable listview
Stephen Fairchild
signupaddress at bethere.co.uk
Sat Mar 8 23:29:35 WST 2008
On Saturday 08 March 2008 09:08:54 Jan Martinek wrote:
> Hello,
>
> I want to make a treeview displaying listview with the possibility of
> sorting and filtering. I must be doing something wrong. This should
> generate numbers from zero through twenty and only even numbers are
> filtered - this works fine.
The code doesn't work because gtk.TreeModel.filter_new() doesn't yield a
subclass of gtk.TreeSortable, which is required by the
gtk.TreeViewColumn.set_sort_column_id() convenience function.
The workaround is to do things the hard way and manage the sorting process
yourself. Unfortunately the sort indicator is not being rendered. Is this a
bug in PyGTK-2.12 or have I overlooked something? YMMV.
#!/usr/bin/python
import gtk, random
ls = gtk.ListStore(int)
values = range(20)
random.shuffle(values)
for i in values:
ls.append((i,))
def rearrange(col, n):
if col.get_sort_order() == gtk.SORT_ASCENDING:
col.set_sort_order(gtk.SORT_DESCENDING)
else:
col.set_sort_order(gtk.SORT_ASCENDING)
ls.set_sort_column_id(n, col.get_sort_order())
#ls.set_sort_column_id(0, gtk.SORT_ASCENDING)
rend = gtk.CellRendererText()
col = gtk.TreeViewColumn('Number', rend, text = 0)
col.set_clickable(True)
col.set_sort_indicator(True)
col.connect("clicked", rearrange, 0)
def visiblefunc(model, iter):
return model.get_value(iter, 0) % 2 == 0
filter = ls.filter_new()
filter.set_visible_func(visiblefunc)
tv = gtk.TreeView(filter)
tv.append_column(col)
w = gtk.Window()
w.add(tv)
w.connect('destroy', gtk.main_quit)
w.show_all()
gtk.main()
--
Stephen Fairchild.
More information about the pygtk
mailing list