GtkTreeviewColumn 每行具有不同的组合模型
我有一组全局值,例如 ["Foo", "Bar", "Baz", "Quux"]
。我的 TreeView
中的每一行都代表一个只能处理这些值的子集的实体。例如,第一行可能处理 "Foo"
和 "Bar"
,第二行可能处理 "Bar"
和 "奎克斯”
。我想要一个 ComboBox 列来允许每一行选择它可以处理的值之一。
但是,从我现在的代码来看,整个列只能有一个 ComboBox 模型:
crc = gtk.CellRendererCombo()
crc.set_property('model', fooValuesModel)
crc.set_property('text-column', 0)
crc.set_property('editable', True)
crc.set_property('has_entry', False)
cl = gtk.TreeViewColumn(ctitle, crc, text=i)
treeView.append_column(cl)
我只有一次机会为整个列设置模型。有没有办法为每一行设置不同的存储,或者以某种方式过滤值?
I have a global set of values, e.g. ["Foo", "Bar", "Baz", "Quux"]
. Each row in my TreeView
represents an entity that can only deal with a subset of these values. For example, the first row might deal with "Foo"
and "Bar"
, and the second, "Bar"
and "Quux"
. I want a ComboBox
column to allow each row to select one of the values it can deal with.
However, from the code I have now, the entire column can only have one model for the ComboBox
:
crc = gtk.CellRendererCombo()
crc.set_property('model', fooValuesModel)
crc.set_property('text-column', 0)
crc.set_property('editable', True)
crc.set_property('has_entry', False)
cl = gtk.TreeViewColumn(ctitle, crc, text=i)
treeView.append_column(cl)
I have only one opportunity to set a model for the entire column. Is there any way to have different stores for each row, or to filter values somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是
gtk.TreeModelFilter
。它是一个树模型,包含另一个底层树模型的过滤值。您可以通过在过滤后的模型上调用set_visible_func()
来决定哪些行应该可见。What you are looking for is
gtk.TreeModelFilter
. It is a tree model containing filtered values of another underlying tree model. You can decide which rows should be visible by callingset_visible_func()
on the filtered model.还有另一种方法可以做到这一点。
参见:
http://mail.gnome.org/archives/gtk -perl-list/2005-July/msg00065.html
(它是用 perl 编写的,但转换为 python 应该不会太困难)
秘密是这样的:
假设您有一个使用 TreeStore 作为其模型的 TreeView。其中一列使用组合框单元格渲染器,它将每行获取一个 ListStore 模型:
您可以将 ListStore 放入 TreeStore 列中,在 Perl 中,这将是
Gtk2::ListStore
类型的列。当然,您不会向 TreeView 添加直接显示此内容的列。相反:当您将组合框单元格列添加到树视图时,可以使用“insert_column_with_attributes”并将“model”属性连接到 TreeStore 的 ListStore 列,让组合框单元格列使用 Treeview 中的该列作为其 ListStore。
当你向TreeView添加一行时,只需将一个完整的ListStore放入TreeStore的ListStore列中即可。然后,您可以使用所需的任何值填充每行 ListStore。
There's also another way to do this.
See also:
http://mail.gnome.org/archives/gtk-perl-list/2005-July/msg00065.html
(It's in perl, but shouldn't be too difficult to convert to python)
The secret being this:
Say you have a TreeView using a TreeStore as its model. And one of the columns uses a combobox cell renderer which will get a ListStore model per row:
You can put ListStores into TreeStore columns, in perl that would be a column of type
Gtk2::ListStore
. Of course you wouldn't add a column to the TreeView displaying this directly. Instead:You can have the comboboxcell-column use that column from the Treeview as its ListStore when you add it to the treeview, using 'insert_column_with_attributes' and connecting the 'model' attribute to the ListStore column of the TreeStore.
When you add a row to the treeview, just put a complete ListStore into the ListStore column of the TreeStore. You can then fill the per-row ListStore with whatever values you want.