I'm trying convert my old plugin to QGIS3 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")

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:

enter image description here

However, if I remove the layer from layer list and add the layer with the same code in the Python console I have no problem with adding a feature.

Any suggestions for what I can try?

  • Works fine for me, perhaps an issue with the build you are using (I tested this with QGIS 33ca8fcb69)? Is this the latest build you are using? – Joseph Feb 20 '18 at 10:27

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.