[prev in list] [next in list] [prev in thread] [next in thread] 

List:       qgis-user
Subject:    Re: [Qgis-user] Add context menu to map canvas
From:       Ujaval Gandhi via QGIS-User <qgis-user () lists ! osgeo ! org>
Date:       2023-08-02 6:58:34
Message-ID: 8d121842-dc64-1b9b-db85-2af9e530db94 () spatialthoughts ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@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@lists.osgeo.org [QGIS-User@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]


[Attachment #5 (text/html)]

<div dir="ltr" gmail_original="1"><div>Put the code block inside initGui(). Tested on \
a small&nbsp;plugin and worked fine&nbsp;<a \
href="https://mailtrack.io/trace/link/d204ab4d25292415574deaa5cfbfef567d34b153?w=cWdpc \
y11c2VyQGxpc3RzLm9zZ2VvLm9yZw&url=https%3A%2F%2Fpastebin.com%2Fraw%2FU55Hbguk&amp;user \
Id=8747767&amp;signature=26d638063caac0c3">https://pastebin.com/raw/U55Hbguk</a></div><div>---<br></div><div><div \
dir="ltr" class="gmail_signature"><div dir="ltr"><div>Ujaval Gandhi</div><div>Spatial \
Thoughts</div><div><a \
href="https://mailtrack.io/trace/link/77965c2ef6002bde576e0b136d4c883ebfebdcaa?w=cWdpc \
y11c2VyQGxpc3RzLm9zZ2VvLm9yZw&url=http%3A%2F%2Fwww.spatialthoughts.com&amp;userId=8747767&amp;signature=34ffd88bbade5fae" \
target="_blank">www.spatialthoughts.com</a></div><div><br></div></div></div></div><br></div><br><img \
width="0" height="0" class="mailtrack-img" alt="" style="display:flex" \
src="https://mailtrack.io/trace/mail/w/cWdpcy11c2VyQGxpc3RzLm9zZ2VvLm9yZw/59c62500b1b69356f80b0a5285238b6bd78675ac.png?u=8747767" \
><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 1, 2023 at \
> 7:47 PM Christoph Jung via QGIS-User &lt;qgis-user@lists.osgeo.org&gt; \
> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; \
> border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div dir="auto"><div \
> dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">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" \
> target="_blank">https://docs.qgis.org/3.28/en/<wbr>docs/pyqgis_developer_<wbr>cookbook/canvas.html#add-<wbr>items-to-map-canvas-<wbr>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" \
> target="_blank">https://github.com/qgis/QGIS-<wbr>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="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span \
> lang="EN-US">from qgis.PyQt.QtWidgets import QMenu<u></u><u></u></span></p><p \
> class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, \
> sans-serif;"><span lang="EN-US">from qgis.gui import \
> QgsMapMouseEvent<u></u><u></u></span></p><p class="MsoNormal" style="margin: 0cm; \
> font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">import \
> json<u></u><u></u></span></p><p class="MsoNormal" style="margin: 0cm; font-size: \
> 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">&nbsp;</span></p><p \
> class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, \
> sans-serif;"><span lang="EN-US">class \
> SmallworldShortcutPlugin:<u></u><u></u></span></p><p class="MsoNormal" \
> style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span \
> lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;<u></u><u></u></span></p><p class="MsoNormal" \
> style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span \
> lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, \
> iface):<u></u><u></u></span></p><p class="MsoNormal" style="margin: 0cm; font-size: \
> 11pt; font-family: Calibri, sans-serif;"><span \
> lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.iface = \
> iface<u></u><u></u></span></p><p class="MsoNormal" style="margin: 0cm; font-size: \
> 11pt; font-family: Calibri, sans-serif;"><span \
> lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.canvas = \
> self.iface.mapCanvas()</span></p><p class="MsoNormal" style="margin: 0cm; \
> font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: \
> 11pt;">&nbsp; &nbsp; &nbsp; &nbsp; \
> self.canvas.<wbr>contextMenuAboutToShow.<wbr>connect(self.<wbr>populateContextMenu)</span></p><p \
> class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, \
> sans-serif;"><span style="font-size: 11pt;"><br></span></p><p class="MsoNormal" \
> style="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="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><br></p><p \
> class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, \
> sans-serif;">Best regards,</p><p class="MsoNormal" style="margin: 0cm; font-size: \
> 11pt; font-family: Calibri, sans-serif;">Christoph&nbsp;</p><p class="MsoNormal" \
> style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span \
> style="font-size: 11pt;"><br></span></p></div></div></div></div></div></div>______________________________<wbr>_________________<br>
> 
QGIS-User mailing list<br>
<a href="mailto:QGIS-User@lists.osgeo.org" \
target="_blank">QGIS-User@lists.osgeo.org</a><br> List info: <a \
href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" \
                target="_blank">https://lists.osgeo.org/mailma<wbr>n/listinfo/qgis-user</a><br>
                
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" \
rel="noreferrer" target="_blank">https://lists.osgeo.org/mailma<wbr>n/listinfo/qgis-user</a><br>
 </blockquote></div>



_______________________________________________
QGIS-User mailing list
QGIS-User@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic