I'm trying to convert my old plugin to QGIS 3, and one part is to create a shapefile, and afterwards load it to the Layer menu like this:
from qgis.core import QgsVectorLayer, QgsProject
layer = QgsVectorLayer('c:/dev/test.shp', 'test', "ogr")
print(layer.isValid())
QgsProject.instance().addMapLayer(layer)
The Python Console returns True
and the Layer appears in the layer list, hence I can't add features to it when I have toggled editing:
However, if I remove a layer from the layer list and add another layer with the same code in the Python Console, I have no problem adding a feature.
Any suggestions for what I can try?
1 Answer 1
I solved the problem, I believe that the layer was loaded without fields in some way. It was wired though, it had an 'ID' column when I checked in the Attribute table, but somehow I believe that it had something todo with error anyway. When I changed the layer creation method (by the help of the code in this question) the layer was loaded correctly from the plugin.
Explore related questions
See similar questions with these tags.