Displaying multiple cellrenderers in a treeviewcolumn

Hi
I am trying to get multiple cellrenders displaying in a tree view column

However it is only displaying one renderer (the last if all renderers are visible)

Sample code (using pixbuf so no complications with model)

       my $view=Gtk3::TreeView->new;
	## Create pixbuf renderers
	my $pixbuf=Gtk3::CellRendererPixbuf->new;
	$pixbuf->set('icon-name','Document-Open');
	my $pixbuf2=Gtk3::CellRendererPixbuf->new;
	$pixbuf->set('icon-name','Document-Open');
	## create column
	my $colt=Gtk3::TreeViewColumn->new;
	$view->append_column($colt);
	##pack renderers
	$colt->pack_start($pixbuf,0);
	$colt->pack_start($pixbuf2,0);

I am sure I am missing something obvious, so any hints appreciated

There is a typo in your sample code. You set icon-name on pixbuf twice, and never on pixbuf2

From what I know, you can’t have multiple cell renderers for the same column be visible at the same time. Actually, they are all visible but you can see only the last one as that one is the last one that gets to draw its content into the cell. (at least, that’s how I think it works)

So, you have to pick, which one you want visible at which point in the life of the cell. You can do this by setting the column’s cell data function (gtk_tree_view_column_set_cell_data_fun()) and manipulating each of the renderers there.

Again, not very familiar with the guts of GtkTreeView but if you really wanted to show all cell renderers at the same time, may be, you can change the rectangle that each renderer is given for its content do not overlap.

You can indeed have multiple cell renderers in a single column. A simple example based on redtux’s:

#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

tvc = Gtk.TreeViewColumn("Icons")

cell1 = Gtk.CellRendererPixbuf()
cell1.props.icon_name = 'document-open'
tvc.pack_start(cell1, False)

cell2 = Gtk.CellRendererText()
cell2.props.text = 'Hello World'
tvc.pack_start(cell2, True)

cell3 = Gtk.CellRendererPixbuf()
cell3.props.icon_name = 'document-save'
tvc.pack_start(cell3, False)

tv = Gtk.TreeView()
tv.append_column(tvc)

ls = Gtk.ListStore(object)
tv.set_model(ls)

for i in range(10):
    ls.append()

w = Gtk.Window()
w.add(tv)
w.show_all()

Gtk.main()

When actually storing data in the model, both Gtk.TreeViewColumn.set_attributes() and Gtk.TreeViewColumn.set_cell_data_func() require you to specify the cell renderer, so you can call them for each one.

1 Like

I suspect (will check for sure when I get home) that if you add renderers to the column after you add the column to the treeview , it behaves as vodtrance believes. However if you add the renders and attributes before adding the column all renderers are visible.

Actually, after some more thought, I realized I was wrong in my understanding. I use multiple renderers in my application and I remember that they all were visible initially. I had to connect a cell data function to manipulate visibility according to the state of the model.

This is true regardless of when the renderers are added:

#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

tvc = Gtk.TreeViewColumn("Icons")

cell1 = Gtk.CellRendererPixbuf()
cell1.props.icon_name = 'document-open'
tvc.pack_start(cell1, False)

cell2 = Gtk.CellRendererText()
cell2.props.text = 'Hello World'
tvc.pack_start(cell2, True)

cell3 = Gtk.CellRendererPixbuf()
cell3.props.icon_name = 'document-save'
tvc.pack_start(cell3, False)

tv = Gtk.TreeView()
tv.append_column(tvc)

tvc2 = Gtk.TreeViewColumn("Second")
tv.append_column(tvc2)

cell4 = Gtk.CellRendererPixbuf()
cell4.props.icon_name = 'document-edit'
tvc2.pack_start(cell4, False)

cell5 = Gtk.CellRendererPixbuf()
cell5.props.icon_name = 'document-print'
tvc2.pack_start(cell5, False)

ls = Gtk.ListStore(object)
tv.set_model(ls)

for i in range(10):
    ls.append()

w = Gtk.Window()
w.add(tv)
w.show_all()

Gtk.main()

Thanks I resolved issue (and expanded it), turned out to be a sizing issue, cellrendererpixbufs dont seem to well behaved in this, or at least behave differently from other pixbufs

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.