When I try to calculate a new field with:
idx = layer.fieldNameIndex(attributeName)
I receive the following error:
AttributeError: 'QgsVectorLayer' object has no attribute 'fieldNameIndex'
What´s the problem? I'm using QGIS 3.0.1 64x in Windows.
When I try to calculate a new field with:
idx = layer.fieldNameIndex(attributeName)
I receive the following error:
AttributeError: 'QgsVectorLayer' object has no attribute 'fieldNameIndex'
What´s the problem? I'm using QGIS 3.0.1 64x in Windows.
Use one of the following methods from the QgsFields class:
layer = iface.activeLayer()
layer_fields = layer.fields()
field_index = layer_fields.indexFromName("FIELD_NAME")
layer = iface.activeLayer()
layer_fields = layer.fields()
field_index = layer_fields.lookupField("FIELD_NAME")
layer = iface.activeLayer()
layer_fields = layer.fields()
field_index = layer_fields.indexOf("FIELD_NAME")
You need to access it via the fieldNameIndex() method of the QgsVectorDataProvider class:
layer = iface.activeLayer()
idx = layer.dataProvider().fieldNameIndex(attributeName)
layer = iface.activeLayer(). Then type idx = layer.dataProvider().fieldNameIndex("fieldName"). Theself` parameter is mainly used for plugins.
– Joseph
Apr 20 '18 at 09:15
self.
– Joseph
Apr 23 '18 at 09:51