[GIS] How to allow users to select features with the mouse

mouse positionpyqgisqgis

I am using PyQGis API to create a custom application.

How do I allow users to select features with the mouse? Is there a mapTool for this? Surely with a product as powerful as QGIS there must be.

Best Answer

There is a class you can inherit from called QgsMapTool which you can override the mouse events to handle user actions.

Like so: https://github.com/NathanW2/qmap/blob/master/src/plugin/point_tool.py

You can find objects under the mouse click for example using code like this:

searchRadius = (QgsTolerance.toleranceInMapUnits( 5, layer, 
               self.canvas.mapRenderer(), QgsTolerance.Pixels))
        rect = QgsRectangle()
        rect.setXMinimum( point.x() - searchRadius );
        rect.setXMaximum( point.x() + searchRadius );
        rect.setYMinimum( point.y() - searchRadius );
        rect.setYMaximum( point.y() + searchRadius );

        self.band.reset()
        layer.select( layer.pendingAllAttributesList(), rect, True, True)    
        for feature in layer:
             # do something with the feature