2

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?

gpoo
  • 8,408
  • 3
  • 38
  • 53
Claudiu
  • 224,032
  • 165
  • 485
  • 680

2 Answers2

3

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 calling set_visible_func() on the filtered model.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • How to trigger the model change? Anything better than `cursor-changed` on the `TreeView`? – bobince Aug 27 '10 at 00:47
  • @bobince, unfortunately not as far as I can think of. You can call `refilter()` on the model whenever you want to change it but I think `cursor-changed` is the best option as to which signal handler to do that in. – ptomato Aug 27 '10 at 06:43
2

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:

  1. 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:

  2. 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.

  3. 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.

gpoo
  • 8,408
  • 3
  • 38
  • 53