[QGIS-Developer] Disabling map context menu via PyQGIS

Johannes Kröger (WhereGroup) johannes.kroeger at wheregroup.com
Fri Sep 3 02:25:53 PDT 2021


Thank you guys so much for your great suggestions!

Since I am not already overriding QgsMapCanvas or using a custom MapTool 
at all times, I went for Benoit's solution (which Benjamin hinted at) 
and it works great.

A hundred internet points to you! :)

Cheers, Hannes

Am 03.09.21 um 09:58 schrieb Benoit Ducarouge:
> Hi,
>
> if you just want to hide the menu, you can clear it on the 
> contextMenuAboutToShow event :
>
> def clearMenu(menu, event):
>     menu.clear()
> iface.mapCanvas().contextMenuAboutToShow.connect(clearMenu)
>
> Benoit.
>
> Le 03/09/2021 à 09:31, Benjamin Jakimow a écrit :
>>
>> Hi Hannes,
>>
>> in C++ you could override
>> void QgsMapCanvas::showContextMenu( QgsMapMouseEvent *event )
>>
>> which creates and shows the context menu.
>>
>> in Python showContextMenu is not available. There you could re-implement
>> void QgsMapCanvas::mousePressEvent( QMouseEvent *e ) to avoid any 
>> calls of showContextMenu()
>>
>> If you just like to modify the context menu, you might add a slot for 
>> the
>> void contextMenuAboutToShow( QMenu *menu, QgsMapMouseEvent *event ) 
>> signal that manipulates the menu.
>>
>> Greetings,
>>
>> Benjamin
>>
>>
>> So you might either override it or
>>
>> to disable the context menu you can override 
>> QgsMapCanvas::mousePressEvent
>>


More information about the QGIS-Developer mailing list