I want to make a custom application which includes an option just like the "Select Features by Polygon" feature in the QGIS. I am able to draw the polygon using following code:

self.rb = QgsRubberBand(self.canvas, QGis.Polygon)
vertex = QgsPoint(point.x(), point.y())

I want to add the feature in which the polygon sort of a sticks to the mouse pointer until the polygon is completed just like the "Select Features by Polygon" in QGIS which is more user friendly. Any ideas on how to do this?

1 Answer 1


It's incomplete but looking at the cookbook part about custom map tools would help you achieve this feature.

To make the sample works (in the QGIS context, not within an application), execute the sample code from the QGIS integrated editor (you need to solve some qgis, PyQt4 python import to run the code smoothly), then just call:

rect_tool = RectangleMapTool(iface.mapCanvas())

Try to draw a rectangle (red) to understand the drawing behaviour.

  • From this drawing, you can get the geometry coordinates.
  • Do the spatial operations you want between the drawn geometry and the layer(s) you want to select
  • After, change code to add vertex for each click instead of using a rectangle selection like for the sample)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.