In a QGIS plugin, I implemented a RectangleMapTool that I apply to my canvas. Thus, the user has the possibility to select a bounding box to define the spatial processing footprint.

Once he has selected his bounding box, I'd like to delete this QgsRectangle but I can't do it. If the user clicks anywhere after drawing the bounding box, the rectangle disappears but sometimes it remains.

enter image description here

You can run the following code in the Python console. If you run the script a second time, the bounding box will remain drawn:

from processing.gui.RectangleMapTool import RectangleMapTool

# Function
def bbox():
    r = tool.rectangle()

# MapTool
canvas = iface.mapCanvas()
tool = RectangleMapTool(canvas)

# QLineEdit
line_bbox = QLineEdit()

I explored the documentation to be able to delete a QgsRectangle. When it is an entity of a layer I know how to do it but in my case, it is drawn on the canvas.

I also explored several links that ran iface.mapCanvas().scene().items(). I tried to remove the QgsRubberBand objects but my QGIS crashes or no changes are made.

  • Does this answer help you? Make sure you use a Python variable (e.g., a dict) to store and delete the items. Jan 14, 2020 at 14:59
  • I read your answer, but in my case I don't have layer sometimes. I draw directly on the canvas. Jan 14, 2020 at 15:05
  • Please provide some code for reproduce your objects and case. Jan 14, 2020 at 15:43
  • I edited my post. I added my code. Jan 14, 2020 at 15:52
  • 1
    You may need to update to new version of QGIS. Crashing issue is fixed in next versions. Link. Jan 14, 2020 at 20:28

1 Answer 1


For me you have just to test if the tool object is already created and call the reset() method for delete the QgsRubberBand displayed :

# MapTool
canvas = iface.mapCanvas()
if tool:

tool = RectangleMapTool(canvas)
  • Thanks a lot! I was looking for too complicated around QgsRectangle and QgsRubberBand while reset works perfectly well. Jan 15, 2020 at 7:30

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.