开发者

gtk.treeviewcolumn not sorting

I've gotten a sortable treeview working. Clicking on the columns makes it sort by ascending order, and clicking it again makes it sort by descending order. However, if I click on the column head开发者_运维知识库er a third time, it goes to some "unsorted" state, instead of back to ascending. I connected a function to the clicked signal of the column, and printed out the column's get_sort_order(), and for each click I get SORT_ASCENDING for ascending, SORT_DESCENDING for descending, and SORT_DESCENDING again for the "unsorted" state. My tree view construction is something like this:

    self.hsModel = gtk.TreeStore(*[c[0] for c in columns])
    self.hsModelFilter = self.hsModel.filter_new()
    self.hsModelSort = gtk.TreeModelSort(self.hsModelFilter)
    #... define filterfunc ...
    self.hsModelFilter.set_visible_func(filterfunc)
    self.hsSelect = gtk.TreeView(self.hsModelSort)

    cl = gtk.TreeViewColumn(ctitle, renderer, **attrcols)                    
    cl.set_clickable(True)
    cl.set_sort_column_id(COL_ACTUALTIME)

    #... define sortdate ...
    self.hsModelSort.set_sort_func(COL_ACTUALTIME, sortdate)
    self.hsModelSort.set_sort_column_id(COL_ACTUALTIME, gtk.SORT_DESCENDING)

I never want to be in this "unsorted" state. I want it to either sort by ascending order or descending order. How do I get rid of the "unsorted" state?


The self.hsModelSort.set_default_sort_func(None) works, but you will lose the original state (previous any sort), if is that what you want then thats the solution, if you want to keep the original form you must set the default function to some function that sort the column to the original state.

Most likely you already sort the values in descending order in the first place so you just need to do:

self.hsModelSort.set_sort_column_id(COL_ACTUALTIME, gtk.SORT_ASCENDING) self.hsModelSort.set_default_sort_func(None)

I hope this clarify my point:

import gtk

w = gtk.Window()
w.resize(300, 300)

cols = [(1,8,3),
        (7,12,9),
        (4,5,6),
        (10,11,12)]

model = gtk.ListStore(int, int, int)
model.set_sort_column_id(0, gtk.SORT_ASCENDING)
model.set_default_sort_func(None )

#sort columns in ascending order for the column 0
cols.sort(lambda x,y: cmp(x[0],y[0]), reverse=True)

for c in cols:
    model.append(c)

tv = gtk.TreeView(model)
tv.set_headers_clickable(True)

columns = ('one', 'two', 'three')
renderer = gtk.CellRendererText()
for i, c in enumerate(columns):
    col = gtk.TreeViewColumn(c, renderer)
    col.add_attribute(renderer, 'text', i)
    col.set_clickable(True)
    col.set_sort_column_id(0)
    col.set_sort_indicator(True)
    tv.append_column(col)

w.add(tv)
w.show_all()
w.connect('destroy', lambda _: gtk.main_quit())
gtk.main()


Didn't test, but try if

self.hsModelSort.set_default_sort_func (None)

helps. Default value is just "use underlying order", but should be possible to reset to "no sort function at all" state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜