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)
        lambda x: print(x)

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.


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:


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())

    def unload(self):
        del self.action
    def canvas_clicked(self, pnt):

    def run(self):

The metadata.txt file contents:

description=Minimal plugin
about=About this minimal plugin
author=Your Name
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.

  • 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.