I am working on a small QGIS Python plugin and want to get the X/Y coordinates from a user click on the canvas. I have a small UI called from button in a toolbar. After filling the UI with information I would like to get the XY coordinates. I am using QGIS 3 with Python 3 and QT5.
I tried to use the steps from
How to get co ordinates of points on mouse click in PyQGIS
but when running it jumps right to the end without calling the display_point function and there is no reaction to mouse clicks.
Are there any changes to the current QGIS version?
My code, which is inside the plugin class and called by a button from the toolbar without UI.
def run2(self):
# a reference to our map canvas
canvas = iface.mapCanvas()
# this QGIS tool emits as QgsPoint after each click on the map canvas
pointTool = QgsMapToolEmitPoint(canvas)
# Checkpoint
print("S 1")
pointTool.canvasClicked.connect( self.display_point )
canvas.setMapTool( pointTool )
# Checkpoint
print("S 3")
def display_point( pointTool ):
print('({:.4f}, {:.4f})'.format(pointTool[0], pointTool[1]))
# Checkpoint
print("S 2")
Best Answer
In a plugin (displayed "Always On Top"), with PyQGIS 3, to avoid it jumps right to the end without calling the display_point function you need following changes in your code:
It works; as it can be observed at following image: