I have a code that reacts to a mouse click. I click on the label of an object and get the ID of this object. Everything works perfectly. But I need to do all the same when some other tool is active ("Move a Label..." tool). Or at worst, so that this code is always executed regardless of the selected tool. Is there a way to achieve this?
def canvasPressEvent(event):
x = event.pos().x()
y = event.pos().y()
point = canvas.getCoordinateTransform().toMapCoordinates(x, y)
labels = iface.mapCanvas().labelingResults().labelsAtPosition(QgsPointXY(point))
if labels:
print (labels[0].featureId)
from qgis.gui import QgsMapToolEmitPoint
canvas = iface.mapCanvas()
pointTool = QgsMapToolEmitPoint(canvas)
pointTool.canvasPressEvent = canvasPressEvent
canvas.setMapTool(pointTool)