10

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:

enter image description here

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?

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Feb 19, 2018 at 16:46
0

1 Answer 1

4

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.

answered Feb 22, 2018 at 8:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.