[Qgis-user] Add context menu to map canvas

Ujaval Gandhi ujaval at spatialthoughts.com
Wed Aug 2 00:00:08 PDT 2023


Put the code block inside initGui(). Tested on a small plugin and worked
fine https://pastebin.com/raw/U55Hbguk
[https://mailtrack.io/trace/link/d204ab4d25292415574deaa5cfbfef567d34b153?url=https%3A%2F%2Fpastebin.com%2Fraw%2FU55Hbguk&userId=8747767&signature=26d638063caac0c3]
---

Ujaval Gandhi
Spatial Thoughts
www.spatialthoughts.com
[https://mailtrack.io/trace/link/77965c2ef6002bde576e0b136d4c883ebfebdcaa?url=http%3A%2F%2Fwww.spatialthoughts.com&userId=8747767&signature=34ffd88bbade5fae]




[data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=3D]
On Tue, Aug 1, 2023 at 7:47 PM Christoph Jung via QGIS-User
<qgis-user at lists.osgeo.org> wrote:

> Hello everyone,
> 
> 
> 
> 
> I try to add a new entry to the map canvas context menu using the following
> two documentations:
> 
> https://docs.qgis.org/3.28/en/docs/pyqgis_developer_cookbook/canvas.html#add-
> items-to-map-canvas-contextual-menu
> [https://docs.qgis.org/3.28/en/docs/pyqgis_developer_cookbook/canvas.html#add-items-to-map-canvas-contextual-menu]
> 
> 
> 
> 
> https://github.com/qgis/QGIS-Documentation/issues/5842
> [https://github.com/qgis/QGIS-Documentation/issues/5842]
> 
> 
> 
> 
> Adding a new menu entry from the python console works like a charm. Now I want
> to provide the context menu through a plugin. But I am not able to connect the
> context menu with a signal declared as a function in the same class, where I
> want to connect it. I receive the message, that my plugin class has no method
> „addMenu“. This is my code snippet:
> 
> 
> 
> 
> from qgis.PyQt.QtWidgets import QMenu
> 
> from qgis.gui import QgsMapMouseEvent
> 
> import json
> 
>  
> 
> class SmallworldShortcutPlugin:
> 
>     
> 
>     def __init__(self, iface):
> 
>         self.iface = iface
> 
>         self.canvas = self.iface.mapCanvas()
> 
>         self.canvas.contextMenuAboutToShow.connect(self.populateContextMenu)
> 
> 
> 
> 
> The function populateContextMenu is the same as in the documentation. But if I
> declare the function outside of the class in the same python file, it works
> very well (of course without calling the function on self). Does anybody has a
> hint for me how to connect the map canvas context menu with a function from a
> class? Or is it the right way to declare the function to be connected with the
> context menu outside of a class?
> 
> 
> 
> 
> Best regards,
> 
> Christoph 
> 
> 
> 
> 
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org [QGIS-User at lists.osgeo.org]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> [https://lists.osgeo.org/mailman/listinfo/qgis-user]
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> [https://lists.osgeo.org/mailman/listinfo/qgis-user]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230802/c2672e81/attachment.htm>


More information about the QGIS-User mailing list