Menu contextuel

Nombre de programmeurs ont besoin d'implémenter des menus contextuels s'ouvrant d'un clic droit de souris sur des objets TreeView. Pour vous faire gagner du temps, nous allons expliquer ici comment faire. À un ou deux points près, cela ressemble tout à fait à un menu contextuel normal tel que décrit dans le chapitre Menus.

X.VII.I. Gestion du signal 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.

This is demonstrated in the Popup Context Menu example.