I'm trying to create a vector-layer (point, shapefile) and open it in my project. It all works fine but once I try to edit that layer later it doesn't work. addFeatures gives me back false and when I try to edit it via the Qgis Tools the adding Points is greyed out. The odd part is, that I can open the layer I created via the Layer-Menu and it works fine. Also when I type in the code to open it in the console the layer is editable. But somehow in my plugin it doesn't work the same way. This is the part of the code that should be relevant:

filename = self.dlg.lineEdit.text() # I get the directory from a lineEdit
dirname = os.path.dirname(filename)
path = dirname + '/name.shp'

project = QgsProject.instance()

fields.append(QgsField('ID', QVariant.Int))

writer = QgsVectorFileWriter( path, 'utf-8', fields, QgsWkbTypes.Point, project.crs(), "ESRI Shapefile")

iface.addVectorLayer(path,"", "ogr")
  • maybe this helps? gis.stackexchange.com/questions/231797/… – DirkB. Jul 25 '18 at 10:41
  • Unfortunately no, I can start editing without problems. But I can't add any features. In console the layer.addFeature(feature) gives back false. Using the Qgis Toolbar edit the add Points is greyed out. But it works when I open the same file in a different way – Chris Jul 25 '18 at 10:56
  • @Chris, what do you mean by this, "But it works when I open the same file in a different way"? – artwork21 Jul 25 '18 at 11:56
  • I can open the shapefile that was created with QgsVectorFileWriter useing the build-in Qgis Layer menu. If I open it "manually" and not from my plugin I can add features. – Chris Jul 25 '18 at 12:15
  • Also when I type in the iface.addVectorLayer(path,"", "ogr") into the console the layer works as intended. Only difference when i do it in the console is that i can't access the lineEdit and have to create the path seperatly. But I don't see where the path could be wrong as it is the same the file was created and it does open the correct layer i just can't add any features to it – Chris Jul 25 '18 at 12:23

Your Answer

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

Browse other questions tagged or ask your own question.