GtkTreeView -display expander on the branch w/o children

Hi, ALL,
I need the subject please.
Even if i call gtk_tree_view_set_show_expanders(TRUE) and add just the branch without any leaves, this branch will not show expanders.

Is it even possible?

Thank you.

Hi,

Yes, the default behavior in gtk is to hide the expander for leaves (and empty branches are technically leaves too).

A solution could be to not rely on gtk’s builtins expanders, but create your own ones:

@gwillems ,
This is kind of ugly and it looks like a workaround.
But i guess it woyld be better than nothing… :grinning:

Can you help with some code please?

Thank you.

Hi,
I presume I will need to use gtk_tree_view_column_add_attribute() to do 1.

But how do I call it?

And I don’t even know how to do 2…

What I mean is - I will have to react on the expand signal and display the appropriate icons, but how?

Thank you.

No, add_attribute() is necessary when the properties are variable and shall be taken from the treemodel. The pixbufs are always the same here, so just set them as normal properties.

Something like this (python code):

# Setup column
c = Gtk.TreeViewColumn()
c.set_expand(True)
c.set_spacing(6)
# Column cell: icon
cr = Gtk.CellRendererPixbuf(pixbuf_expander_open=pix_open, pixbuf_expander_closed=pix_closed)
c.pack_start(cr, False)
# Column cell: text
cr = Gtk.CellRendererText()
c.pack_start(cr, True)
c.add_attribute(cr, 'text', 0)  # get label from index 0 of the treemodel
# Add column
treeview.append_column(c)

Gtk should automatically pick the correct one, depending on the expansion state, no need to add special logic.

The only remaining point here is how to create the pixbufs pix_open and pix_closed… Forgot how to get them from icon theme, I mostly use paintables nowadays…

@gwillems ,
Thx for the code.
I see you use 2 calls to pack_start(), but you don’t call pack_end().

Is it how it should be?

Thank you.

That’s up to you :slight_smile:
use pack_start to align the renderer at the start of the column (i.e. left side when using an english locale), and pack_end to align at the end (i.e. right side for english).

@gwillems ,

I also presume that this code needs to be in the gtk_tree_store_append()``` fimction, right?

Thank you.

The code sample I provided shall be run only once to initialize the treeview and its columns layout.

gtk_tree_store_append() is to populate the model, I don’t think you have to change anything there, just keep the same model as you used before.

My example above expects a string at index 0 of the treestore:
c.add_attribute(cr, 'text', 0)
but of course, you have to adapt to what you want to display.

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