[pygtk] dynamic model in treeview edited signal callback?

Darren Hart darren at dvhart.com
Mon Mar 3 17:28:43 WST 2008


I have a composite TreeView class which I use one of two ListStores as the 
model.  In the application, if I would change to the alternate model 
(project_store), the treeview would update correctly, but when I would edit 
one of the rows, my callback would get passed a row from the first model.  I 
changed the code in on_filter_edited() below to use widget.get_model() 
instead of relying on the model passed by the signal.

Having pasted the code in here, I think I see the problem, but not the 
solution.  I think I am statically binding the callback to use the first 
model (in the connect('edited' ...).  How should I handle this?  I can pass 
None as the model... perhaps a lambda function?  Is it possible to ensure I 
get the correct model passed, or do I have to ignore the model parameter in 
the callback, and get the model myself?

class TaskFilterListView(WidgetWrapper):
    def __init__(self, widget, context_store, project_store):
        WidgetWrapper.__init__(self, widget)
        self.context_store = context_store
        self.project_store = project_store
        self.widget.set_model(self.context_store)

        # setup the column and cell renderer
        self.tvcolumn0 = gtk.TreeViewColumn()
        self.cell0 = gtk.CellRendererText()
        self.cell0.set_property('editable', True)
        self.cell0.connect('edited', self.on_filter_edited, 		
			   self.widget.get_model(), 0)
        self.tvcolumn0.pack_start(self.cell0, False)
        self.tvcolumn0.set_cell_data_func(self.cell0, self.data_func, "data")
        self.widget.append_column(self.tvcolumn0)

    def filter_by_context(self):
        self.widget.set_model(self.context_store)

    def filter_by_project(self):
        self.widget.set_model(self.project_store)

    def on_filter_edited(self, cell, path, new_text, model_bad, column):
        model = self.widget.get_model()
        model[path][column].title = new_text

-- 
Darren Hart


More information about the pygtk mailing list