In this topic https://gis.stackexchange.com/a/430301/192997 it is explained how to run a basic function from a canvas event :
Code edited by changing
QgsMapToolbyQgsMapToolPan:
def test(event):
print('canvas release')
tool = QgsMapToolPan(iface.mapCanvas())
tool.canvasReleaseEvent = test
iface.mapCanvas().setMapTool(tool)
I try to do the same to run a function from a plugin (made with PluginBuilder) with canvasReleaseEvent:
class MyPlugin:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor."""
self.iface = iface
self.dlg = MyPluginDialog()
#code......
def tr(self, message):
#code......
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):
#code......
def initGui(self):
#code......
def unload(self):
#code......
def run(self):
if self.first_start == True:
self.first_start = False
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
tool = QgsMapToolPan(self.iface.mapCanvas())
tool.canvasReleaseEvent = lambda event self.function_from_plugin(event)
self.iface.mapCanvas().setMapTool(tool)
def function_from_plugin(self, event):
if self.dlg.horizontalSlider.value() == 1:
### rest of the code
but I get this error : AttributeError: 'QgsMapMouseEvent' object has no attribute 'dlg'
I looked other topics but I can't manage to understand everything:
How to link my QGIS plugin main class to mouse events from a custom Tool?
https://gis-ops.com/qgis-3-plugin-tutorial-pyqt-signal-slot-explained/
ifacein the script,__init__method have to includeself.iface = iface– Kadir Şahbaz Oct 13 '22 at 08:31