I am trying to execute a code on Left Click and Right Click should stop code execution and reopen the plugin dialog.
My code: This class is created for mouse click on the map. Here I do not really understand the way to stop plugin, instead I am trying to relaunch plugin dialog window to be able to add some variables in it later.
class PointTool(QgsMapTool):
def __init__(self, canvas):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.iface = iface
def canvasReleaseEvent(self, event):
#Get the click
if event.button() == QtCore.Qt.RightButton:
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
x1=point[0]
y1=point[1]
self.iface.messageBar().pushMessage("RIGHT BUTTON COORDINATES: "+str(round(point[0],1))+","+str(round(point[1],1))+"</a>", duration=7)
##### HERE I AM TRYING TO RELAUNCH THE PLUGIN DIALOG
rightaction = myplag(iface)
rightaction.run()
elif event.button() == QtCore.Qt.LeftButton:
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
x1=point[0]
y1=point[1]
self.iface.messageBar().pushMessage("LEFT BUTTON COORDINATES: "+str(round(point[0],1))+","+str(round(point[1],1))+"</a>", duration=7)
and below is a standard plugin class that I edited in def run(self):
class myplag:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'myplag_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&myplag')
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
# noinspection PyMethodMayBeStatic
def tr(self, message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('myplag', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
# Adds plugin icon to Plugins toolbar
self.iface.addToolBarIcon(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/myplag/icon.png'
self.add_action(
icon_path,
text=self.tr(u''),
callback=self.run,
parent=self.iface.mainWindow())
# will be set False in run()
self.first_start = True
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&myplag'),
action)
self.iface.removeToolBarIcon(action)
# def display_point(pointTool):
# try:
# print(pointTool.x(), pointTool.y())
# except AttributeError:
# pass
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.dlg = myplagDialog()
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# a reference to our map canvas
tool = PointTool(iface.mapCanvas())
iface.mapCanvas().setMapTool(tool)
When I Right click, it gives me error:
'myplag' object has no attribute 'dlg'
1 Answer 1
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 and simply show it on the right mouse click using self.parent.show()
.
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: