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)
class PointTool(QgsMapTool): NameError: name 'QgsMapTool' is not defined. Any ideas? – robert Jan 03 '13 at 12:34from qgis.gui import QgsMapToolat the top – Nathan W Jan 03 '13 at 12:39None. I would save what the user had selected usingQgsMapCanvas.mapTool()restoring it after you are done. – Nathan W Jan 03 '13 at 13:01tool = PointTool(self.iface.mapCanvas())andself.iface.mapCanvas().setMapTool(tool)– wannik Nov 23 '14 at 04:38point = e.mapPoint()is enough, it even supportse.snapPoint()http://qgis.org/api/classQgsMapMouseEvent.html. – Matthias Kuhn Jan 21 '16 at 09:04