<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8">Hello everyone,<div dir="ltr"></div><div dir="ltr"></div><div><br></div><div>I try to add a new entry to the map canvas context menu using the following two documentations:</div><div><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;"><a href="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</a></p><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;"><br></p><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;"><a href="https://github.com/qgis/QGIS-Documentation/issues/5842">https://github.com/qgis/QGIS-Documentation/issues/5842</a></p><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;"><br></p><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;">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:</p><p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica;"><br></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">from qgis.PyQt.QtWidgets import QMenu<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">from qgis.gui import QgsMapMouseEvent<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">import json<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"> </span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">class SmallworldShortcutPlugin:<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">    <o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">    def __init__(self, iface):<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">        self.iface = iface<o:p></o:p></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">        self.canvas = self.iface.mapCanvas()</span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 11pt;">        self.canvas.contextMenuAboutToShow.connect(self.populateContextMenu)</span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 11pt;"><br></span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 11pt;">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?</span></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><br></p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">Best regards,</p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">Christoph </p><p class="MsoNormal" style="-webkit-text-size-adjust: auto; margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 11pt;"><br></span></p></div></div></div></div></div></body></html>