gtk.treeviewcolumn 不排序

发布于 2024-09-27 01:22:07 字数 990 浏览 4 评论 0原文

我已经得到了一个可排序的树视图。单击列可按升序排序,再次单击列可按降序排序。但是,如果我第三次单击列标题,它会进入某种“未排序”状态,而不是返回升序。我将一个函数连接到列的 clicked 信号,并打印出列的 get_sort_order(),每次点击我都会得到 SORT_ASCENDING升序,SORT_DESCENDING 表示降序,再次 SORT_DESCENDING 表示“未排序”状态。我的树视图构造是这样的:

    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'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 header 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-10-04 01:22:07

self.hsModelSort.set_default_sort_func(None) 有效,但是您将丢失原始状态(之前的任何排序),如果这是您想要的,那么这就是解决方案,如果您想保留原始形式您必须将默认函数设置为将列排序为原始状态的函数。

很可能您已经首先按降序对值进行排序,因此您只需要执行以下操作:

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

我希望这能澄清我的观点:

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()

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()
独行侠 2024-10-04 01:22:07

没有测试,但尝试一下是否

self.hsModelSort.set_default_sort_func (None)

有帮助。默认值只是“使用基础顺序”,但应该可以重置为“根本没有排序功能”状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文