Gtk TreeView popup menu

Hello, how would you guys implement right click menu on TreeView items?

I’ve found some links which just react to right click on the TreeView object, but I would like more to attach the menu to single ~rows~. Can you just point me in the right direction? Which objects should I use, which binding, etc?

Old tutorials were using Gtk.Menu, but I think Gtk.Popup is much more cooler … :smiley:

Regards,
Ondrej

I connect to the “button-release-event” signal on the treeview and then use (in Python):

def treeview_button_release_event (self, treeview, event):
    if event.button == 3:
        #show your Popover

I am not sure what you mean by attaching the menu to a row.

Thanks for quick answer @theGtknerd! This is what I have

My point is:
The popupmenu I am getting is relative to the TreeView, but I would like to relate it to the “content rows”. I was able to get that working with Gtk.Menu, but AFAIK Gtk.Popover is more modern way.

Thanks for helping me out. :heart_eyes:

This seems to work for me:

def treeview_button_release_event (self, treeview, event):
	if event.button == 3:
		rect = Gdk.Rectangle()
		rect.x = event.x
		rect.y = event.y
		rect.width = rect.height = 1
		popover = Gtk.Popover()
		label = Gtk.Label(label='theGtknerd')
		popover.add(label)
		popover.set_relative_to(treeview)
		popover.set_pointing_to(rect)
		popover.show_all()
		#popover.popup()

I used several different sources of info to get this working

1 Like

Thats perfect!

It will probably cause some problems in the future, as I had to move to y point a bit, but it works great! Thanks for helping.

1 Like

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