Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top