Unable to get CellRenderer working correctly



Hi,

I tried to customize a Gtk.TreeColumn by setting a "cell data function"
to convert the value from my model to something more useful for my
renderer (converting a path nam to an actual pixbuf).

It wasn't working so I tried more or less the same with a much simpler
model, but it still doesn't work. This is basically what I tried:

    def cell_data_func(column, cell, model, iter, data):
        cell.set_property('text', model.get(iter, 0))

    store = Gtk.ListStore(str)
    view = Gtk.TreeView(store)

    column = Gtk.TreeViewColumn("Value")
    renderer = Gtk.CellRendererText()
    column.set_cell_data_func(renderer, cell_data_func)
    view.append_column(column)

    ...

And the tree view stays empty.

I got the same problem if the renderer is configured in the column using
``.add_attribute()``: nothing get rendered.

Finally, I got a segfault if, in my first example, I *do not* save the
renderer instance, but I pass it directly to ``.set_cell_data_func()``:

    column.set_cell_data_func(Gtk.CellRendererText(), cell_data_func)

So, I was thinking doing something weird, but that's a lot of things
which I would do wrong, so I'm wondering what's going on :)
I made a runnable example which displays 3 columns from the same model,
but each column is set up differently: only the first one get rendered.
Also, there's the segfault problem which can be uncommented.

I'm using a recent version of PyGObject from Git
(60544b02f6f98c0b212625ae83b94a4c6debddeb) but got the same problem on
older version (the onee if Debian sid, 3.2.2-1)

 Jon
from gi.repository import Gtk

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)


def cell_data_func(column, cell, model, iter, data):
    value = model.get(iter, 0)
    cell.set_property('text', str(value))


store = Gtk.ListStore(str)

view = Gtk.TreeView()

############################################################

##################### Example 1
column_1 = Gtk.TreeViewColumn("Value 1", Gtk.CellRendererText(), text=0)
view.append_column(column_1)


##################### Example 2
column_2 = Gtk.TreeViewColumn("Value 2")
renderer_v2 = Gtk.CellRendererText()
# This segfault:
#column_2.set_cell_data_func(Gtk.CellRendererText(), cell_data_func)
column_2.set_cell_data_func(renderer_v2, cell_data_func)
view.append_column(column_2)


##################### Example 3
column_3 = Gtk.TreeViewColumn("Value 3")
column_3.add_attribute(Gtk.CellRendererText(), "text", 0)
view.append_column(column_3)

############################################################

view.set_model(store)

viewport = Gtk.ScrolledWindow()
viewport.add(view)

window = Gtk.Window()
window.add(viewport)
window.show_all()


for i in range(1000):
    store.append((str(i), ))


Gtk.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]