QGIS – Generating Invalid Vector Layer Troubleshooting

invalid-dataqgis

I need an invalid layer to test a plugin in QGIS. In the plugin window, I select a layer from combobox. So, the invalid layer should be in Layers panel. I google for a while but I cannot find a way to generate an invalid vector layer.

How can I generate one so that layer.isValid() returns False?

Best Answer

  • Providing an invalid path makes it invalid: layer = QgsVectorLayer("zxcxzcxc/home/bera/GIS/Data/testdata/ak_riks.shp", "testlayer_shp", "ogr")
  • Or a providerlib that doesnt exist: layer = QgsVectorLayer("/home/bera/GIS/Data/testdata/ak_riks.shp", "testlayer_shp", "O G R"), it should be "ogr"

Then add layer to the project:

QgsProject.instance().addMapLayer(layer)

enter image description here