Sunday 24 June 2018

pyqgis - PyGIS remove QgisVertexMarker from scene


I am writing a plugin that includes interaction with the map. Therefore I use a QgsMapTool. It is possible for a user to mark a point on the map. When the user clicks a red X is drawn on the mapCanvas via QgsVertexMarker.


vertex_marker = QgsVertexMarker(self.canvas)
vertex_marker.setCenter(QgsPoint(map_coordinates['x'], map_coordinates['y']))
vertex_marker.setColor(QColor(255, 0, 0))
vertex_marker.setIconSize(7)
vertex_marker.setIconType(QgsVertexMarker.ICON_X) # ICON_BOX, ICON_CROSS, ICON_X

vertex_marker.setPenWidth(2)

This is working fine. And the red X's are displayed on the map. But I can't delete the VertexMarker. I am trying to delete the VertexMarker from the scene. I've used something like this:


vertex_items = [ i for i in iface.mapCanvas().scene().items() if issubclass(type(i), qgis.gui.QgsVertexMarker)]

for ver in vertex_items:
if ver in iface.mapCanvas().scene().items():
iface.mapCanvas().scene().items().remove(ver)

iface.mapCanvas().refresh()


Like this I get the used VertexMarker that are visible on the scene. But the remove function somehow doesn't remove the marker from the scene.


Is there another possibility to remove the QgsVertexMarker from the scene?



Answer



I found a solution. Instead of


iface.mapCanvas().scene().items().remove(ver) 

the item has to be deleted directly from the scene:


vertex_items = [ i for i in iface.mapCanvas().scene().items() if issubclass(type(i), qgis.gui.QgsVertexMarker)]


for ver in vertex_items:
if ver in iface.mapCanvas().scene().items():
iface.mapCanvas().scene().removeItem(ver)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...