There are a few problems with your code. Below is a minimal plugin example which should help you out. You do not want to try to relaunch your plugin (create a new instance of your plugin class). Instead, you should add a parent
parameter to your map tool class constructor and pass in the plugin dialog when you instantiate the map tool class. This will allow you to access the dialog and it's widgets from inside the map tool class using self.parent
.
I have added some comments to the code below which is based on the minimal plugin example from Martin Dobias here.
It is a simple, working example with all classes and methods within the __init__.py
file and this example shows how I prefer to structure my plugins. I have also included the metadata.txt
file so that you you can save both these files in a folder and copy to your plugins directory, then activate in plugin manager to test.
from qgis.gui import QgsMapTool
from qgis.core import Qgis
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QAction, QDialog, QLabel, QPushButton, QVBoxLayout
def classFactory(iface):
return MapToolPlugin(iface)
# Main Plugin class
class MapToolPlugin:
def __init__(self, iface):
self.iface = iface
self.canvas = self.iface.mapCanvas()
self.dlg = MapToolPluginDialog()
# pass in plugin dialog as parent (1st argument) to map tool constructor
self.map_tool = ExampleMapTool(self.dlg, self.iface, self.canvas)
def initGui(self):
self.action = QAction('Go!', self.iface.mainWindow())
self.action.triggered.connect(self.run)
self.iface.addToolBarIcon(self.action)
# connect dialog button signals to slot methods here (in initGui() method)
self.dlg.ok_btn.clicked.connect(self.set_map_tool)
def unload(self):
self.iface.removeToolBarIcon(self.action)
del self.action
def run(self):
self.dlg.lbl.setText('')
self.dlg.show()
def set_map_tool(self):
self.canvas.setMapTool(self.map_tool)
self.dlg.hide()
class MapToolPluginDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setGeometry(200, 200, 600, 250)
self.lbl = QLabel('Get clicked coordinates', self)
self.ok_btn = QPushButton('Set Map Tool', self)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.lbl)
self.layout.addWidget(self.ok_btn)
# Map tool class; takes parent & iface parameters in constructor
class ExampleMapTool(QgsMapTool):
def __init__(self, parent, iface, canvas):
self.parent = parent
self.iface = iface
self.canvas = canvas
QgsMapTool.__init__(self, self.canvas)
def canvasReleaseEvent(self, event):
click_point = event.mapPoint()
posx = click_point.x()
posy = click_point.y()
if event.button() == Qt.RightButton:
# access self.parent object
self.parent.lbl.setText(f'Click Position: {posx}, {posy}')
# access self.iface object
self.iface.actionPan().trigger()
# call show() on parent object (the plugin dialog)
self.parent.show()
elif event.button() == Qt.LeftButton:
self.iface.messageBar().pushMessage(f'Click Position: {posx}, {posy}',
Qgis.Info, 5)
Metadata file:
[general]
name=MapToolPlugin
description=Map Tool plugin
about=Trivial example of a plugin with a map tool
version=1.0
qgisMinimumVersion=3.0
author=Ben Wirf
[email protected]
repository=URL to the code repository
Short screencast showing result: