Menú contextual emergente

Mucha gente necesita implementar menús contextuales del botón derecho para los TreeView, por lo que se explicará cómo hacerlo para ahorrarle tiempo. Excepto uno o dos puntos, es muy parecido a un menú contextual normal, tal como se describe en el capítulo acerca de menús.

10.7.1. Manejar button_press_event

To detect a click of the right mouse button, you need to handle the button_press_event signal, and check exactly which button was pressed. Because the TreeView normally handles this signal completely, you need to either override the default signal handler in a derived TreeView class, use connect_notify() or use connect(slot, /* after= */ false). You probably also want to call the default handler before doing anything else, so that the right-click will cause the row to be selected first.

Esto queda demostrado en el ejemplo del menú emergente personalizado.