On 7 May 2011 06:39, Jason Heeris <[email protected]> wrote:
> On 7 May 2011 03:24, Neil Muller <[email protected]> wrote:
>> On 6 May 2011 21:07, Jason Heeris <[email protected]> wrote:
>> Storing the object in a hidden column is fine, and what I assumed you
>> were doing originally.
>
> Hah! Okay then, now that it's morning, I can't believe that didn't
> occur to me :P
>
> While we're on the topic though, it strikes me as more sensible to
> simply put the objects themselves in a ListStore without manually
> extracting the information for the other columns (ie. so the ListStore
> just contains one column, which is displayed in different ways). Is
> there a way to tell each TreeViewColumn to, say, "look at column zero,
> take the x.fit.param_A attribute, format it using "0x%03X and display
> it in a CellRendererText" — and then so on for each attribute, each
> describing to a different view of the same column?

You can do via TreeViewColumn.set_cell_data_func and calling
set_property on the cell renderers.

Because the extra bouncing back and forth between gtk and python this
approach involves it does impose a bit of a speed penalty, although
I've used this approach with models holding 100 000 rows without the
slowdowns being an issue.

Simple example:

import gtk, gobject

class MyData(object):
    def __init__(self, value):
        self.number = value
        self.doubled = 2*value

class Test(gtk.Window):
    def __init__(self):
        super(Test, self).__init__()

        store = gtk.ListStore(gobject.TYPE_PYOBJECT)
        store.append([MyData(2)])
        store.append([MyData(3)])
        store.append([MyData(5)])

        self.tv = gtk.TreeView(store)
        self.add(self.tv)
        self.add_column('number')
        self.add_column('doubled')

        self.connect('destroy', lambda *w: gtk.main_quit())
        self.show_all()

    def add_column(self, name):
        cr = gtk.CellRendererText()
        tc = gtk.TreeViewColumn(name, cr)
        tc.set_cell_data_func(cr, self.render, name)
        self.tv.append_column(tc)

    def render(self, column, cell, model, tree_iter, attr_name):
        data = model.get_value(tree_iter, 0)
        cell.set_property("text", "%d" % getattr(data, attr_name))

if __name__ == "__main__":
    t = Test()
    gtk.main()


-- 
Neil Muller
[email protected]

I've got a gmail account. Why haven't I become cool?
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to