Question

I am trying to code the following: Two Columns. One contains a itemId, the other one contains a typeId. I want to render the itemId only when the typeId equals a specific value.


class IDRenderer(gtk.CellRendererText):

  def __init__(self):
    gtk.CellRendererText.__init__(self)

  def do_render(self,window, widget, background_area, cell_area, expose_area, flags):
    if ----} Condition to ask for value of the typeId - Cell {-----:
      gtk.CellRendererText.do_render(self, window, widget, background_area, cell_area,    
                                     expose_area, flags)

gobject.type_register(IDRenderer)

I don't know how to get the iter of the currently rendered row which i need to determine the value of the typeId. Is this even possible?

Was it helpful?

Solution

I now found out, thanks to a nice guy on #pygtk on gimpIRC:

You can do that, with binding so called cell data functions to the corresponding gtk.TreeViewColumn as done here in this example

def renderId(celllayout, cell, model, iter):
  if model.get_value(iter,1) == 3:
    cell.set_property('visible',True)
  else:
    cell.set_property('visible',False)

treeviewcolumn = gtk.TreeViewColumn()
renderer = gtk.CellRendererText()
treeviewcolumn.add_attribute(renderer,'text',0)
treeviewcolumn.set_cell_data_func(renderer,renderId)

I ommited some code relevant to render a complete treeview, but i think it shows what i wanted to do and how to do it.

The column renderes the value in the first column (0) of the model only if the value in the second modelcolumn (1) equals 3

I hope this could help someone some time.

OTHER TIPS

It's not possible as far as I know. You need to use properties of the custom renderer which will be set automatically by the code calling the rendering function. (Like the text property of CellRendererText -- the rendering code doesn't get the text from the tree model, but the tree model sets the text property of the renderer before calling the rendering code.)

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