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.
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()
line_bbox.setText(str(r.xMinimum()))
line_bbox.show()
# MapTool
canvas = iface.mapCanvas()
tool = RectangleMapTool(canvas)
tool.rectangleCreated.connect(bbox)
canvas.setMapTool(tool)
# QLineEdit
line_bbox = QLineEdit()
line_bbox.hide()
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.
