I'm having problems to understand why my custom Gtk.CellRenderer
is only rendering the first row of a Gtk.ListStore
.
I've been reading many docs and trying stuff like cellrenderer.set_visible(True)
but I still have no idea of why this is happening.
Here is a full example:
from gi.repository import Gtk, Gdk, cairo, Pango, PangoCairo, GObject
import time
class CellRenderer5Stars(Gtk.CellRenderer):
__gproperties__ = {
'rating': ( int, # type
"integer prop", # nick
"A property that contains an integer", # blurb
0, # min
5, # max
0, # default
GObject.PARAM_READWRITE # flags
),
}
def __init__(self):
super().__init__()
self.font_size=15
self.font="Sans Bold {}".format(self.font_size)
self.rating = 5
def do_set_property(self, pspec, value):
setattr(self, pspec.name, value)
def do_get_property(self, pspec):
return getattr(self, pspec.name)
def do_get_size(self, widget, cell_area):
return (0, 0, self.font_size*5, self.font_size+5)
def do_render(self, cr, widget, background_area, cell_area, flags):
cr.translate (0, 0)
layout = PangoCairo.create_layout(cr)
desc = Pango.font_description_from_string (self.font)
layout.set_font_description(desc)
stars_var = self.rating
for i in range(5):
if i < stars_var:
layout.set_text("★", -1)
else:
layout.set_text("☆", -1)
cr.save()
PangoCairo.update_layout (cr, layout)
cr.move_to (i*(self.font_size+1), 0)
PangoCairo.show_layout (cr, layout)
cr.restore()
GObject.type_register(CellRenderer5Stars)
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect('destroy', self.on_quit)
liststore = Gtk.ListStore(int)
liststore.append([3])
liststore.append([2])
liststore.append([1])
treeview = Gtk.TreeView(liststore)
treeviewcolumn = Gtk.TreeViewColumn("Rating")
treeview.append_column(treeviewcolumn)
cellrenderer = CellRenderer5Stars()
treeviewcolumn.pack_start(cellrenderer, True)
treeviewcolumn.add_attribute(cellrenderer, "rating", 0)
self.add(treeview)
self.show_all()
def on_quit(self):
Gtk.main_quit()
w = Window()
Gtk.main()
You're rendering your stars to the same place of the cairo surface for every item you pass and don't obey the cell_area
you should use. Replacing
cr.move_to (i*(self.font_size+1), cell_area.y)
in your code will yield a result you'd expect. But the documentation for do_render()
gives you a little more info about the spacing to use:
Invokes the virtual render function of the
Gtk.CellRenderer
. The three passed-in rectangles are areas incr
. Most renderers will draw withincell_area
; the xalign, yalign, xpad, and ypad fields of theGtk.CellRenderer
should be honored with respect tocell_area
.background_area
includes the blank space around the cell, and also the area containing the tree expander; so thebackground_area
rectangles for all cells tile to cover the entirewindow
.
Additionally the 'destroy'
signal has a window
argument, so you should define on_quit(self, window)
or similar instead.