Add gtk.TreeView columns to a size group

我的梦境 提交于 2019-12-08 09:25:19

问题


I want to stack two treeviews on each other and have the columns be aligned. I figured the way to do this would be to use a gtk.SizeGroup somehow. However, gtk.TreeViewColumn is not a widget... how can I do this?


回答1:


I have two suggestions:

  1. Look at how gtk.SizeGroup is implemented and see if you can write your own TreeViewColumnSizeGroup.
  2. Connect to notify::width of each column and in the callback set the width of the corresponding column in the other treeview.



回答2:


UPDATE: This is the final code that worked. In a loop I'm building both view columns at the same time, so this line is sufficient:

col1.connect("notify::width", lambda col1,_,col2:col2.set_fixed_width(
    col1.get_width()), col2)

I think the reason there is no "column widget" is that the main area is just a gtk.gdk.Drawable where each of the cell renderers draw their stuff. However, each column has headers that are widgets, so we can use those to do what we want.

Pick one view to be the 'main' one, and set the other to have gtk.TREE_VIEW_COLUMN_FIXED sizing. Use .forall() to go through the internal child widgets of the 'main' view. These will be gtk.Buttons representing the column headers. Connect to their size-allocate event. On that event handler, get the requested width, and .set_fixed_width of the corresponding column on the slave view.

    self._svcols = []
    def sizealloc(wid, alloc):
        ci = self._svcols.index(wid)
        cl = self.slaveView.get_column(ci)
        cl.set_fixed_width(alloc.width)

    def resizes(child):
        child.connect('size-allocate', sizealloc)
        self._svcols.append(child)

    self.mainView.forall(resizes)

This works even if the column headers are not being shown.



来源:https://stackoverflow.com/questions/3992602/add-gtk-treeview-columns-to-a-size-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!