Monday 1 July 2019

How to remove a vector QgsVectorLayer from QGis python plugin


I managed to add QgsVectorLayer in QGIS through a python plugin, for example:


vl = self.iface.addVectorLayer(uri.uri(), layerName, self.dbConn.getProviderName())


But how can I remove it?



Answer



Are you on QGIS 1.8? If so, you can remove your vl with:


QgsMapLayerRegistry.instance().removeMapLayers( [vl.id()] )

otherwise:


QgsMapLayerRegistry.instance().removeMapLayer( vl.id() )

And if you are on QGIS 3.0+, replace QgsMapLayerRegistry with QgsProject.


Or if by chance you want to remove all empty layers, check out the Remove Empty Layers plugin.



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...