1

I'm trying to use a custom MapTool in my QGIS Plugin. Somehow it seems to deactivate right after I set it. My Plugin is really simple with just one button calling the function below.

    mc = iface.mapCanvas()
    mt = QgsMapToolEmitPoint(mc)
    mt.setButton(self.pushButton)
    mt.canvasClicked.connect(
        lambda x: print(x)
    )
    mc.setMapTool(mt)

I tried listening to the MapTool Changes in the pygis Console using

iface.mapCanvas().mapToolSet.connect(lambda x: print(x))

It shows that the MapTool is activated but also prints None right afterwards as the MapTool is instantly deactivated. I tried setting different MapTools but whenever I try to do it from the Plugin it behaves this way.

2

You haven't shown us how you plugin is structured, or whether you are using Plugin Builder, so I will try to to provide you with an easy to understand example of of a working minimal plugin structure which does basically what you want which you can adapt as you want. This is based on the qgis-minimal-plugin example from Martin Dobias here:

https://github.com/wonder-sk/qgis-minimal-plugin

I think that you should declare both your map canvas and map tool objects as instance attributes of you main plugin class. Also when accessing the qgisinterface object inside a plugin you should use self.iface as all plugin classes should take the iface object in their constructor and save a reference as self.iface.

the __init__.py file contents below:

from PyQt5.QtWidgets import QAction, QMessageBox
from qgis.gui import QgsMapToolEmitPoint

def classFactory(iface):
    return MinimalPlugin(iface)


class MinimalPlugin:
    def __init__(self, iface):
        self.iface = iface
        self.mc = self.iface.mapCanvas()
        self.mt = QgsMapToolEmitPoint(self.mc)
        self.msg = QMessageBox()

    def initGui(self):
        self.action = QAction('Click!', self.iface.mainWindow())
        self.action.triggered.connect(self.run)
        self.iface.addToolBarIcon(self.action)
        
        self.mt.canvasClicked.connect(self.canvas_clicked)

    def unload(self):
        self.iface.removeToolBarIcon(self.action)
        del self.action
        
    def canvas_clicked(self, pnt):
        self.msg.setText(str(pnt))
        self.msg.show()

    def run(self):
        self.mc.setMapTool(self.mt)

The metadata.txt file contents:

[general]
name=MinimalPlugin
description=Minimal plugin
about=About this minimal plugin
version=1.0
qgisMinimumVersion=3.0
author=Your Name
email=your.email@gmail.com
repository=URL to the code repository

You can save both these files in the same folder and copy the folder into your plugins directory to test. Just restart QGIS and enable the plugin in the Plugin Manager.

2
  • I guess my initial Post was missing some information. Yes I use the Plugin Builder :) Thanks to your answer I now have a working version.
    – tristan
    Jul 8 '21 at 8:58
  • Great! If my answer helped solve your problem would you mind accepting it by clicking the check mark? Cheers :) gis.stackexchange.com/help/someone-answers
    – Ben W
    Jul 8 '21 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.