Programatically check for mouse click in PyQGIS?
The best way to make a new tool like the Select Single Feature tool is to inherit from the QgsMapTool
class. When your tool is active, which can be set using QgsMapCanvas::setMapTool
, any keyboard or click events the canvas gets will be passed onto your custom tool.
Here is a basic QgsMapTool
class
class PointTool(QgsMapTool):
def __init__(self, canvas):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
def canvasPressEvent(self, event):
pass
def canvasMoveEvent(self, event):
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
def canvasReleaseEvent(self, event):
#Get the click
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
def activate(self):
pass
def deactivate(self):
pass
def isZoomTool(self):
return False
def isTransient(self):
return False
def isEditTool(self):
return True
You can do what you need in canvasReleaseEvent
, etc
To set this tool active you just do:
tool = PointTool(qgis.iface.mapCanvas())
qgis.iface.mapCanvas().setMapTool(tool)
I think you can do this with a combination of using QGIS "canvasClicked" but also SIGNAL/SLOTS to deal with the response:
result = QObject.connect(self.clickTool, SIGNAL("canvasClicked(const QgsPoint &, Qt::MouseButton)"), self.handleMouseDown)
Not tried but should give you some more information to start looking at. There is a tutorial here where someone is using this to build a very basic plugin.