5

When I try to calculate a new field. I receive the following error:

idx = self.layer.fieldNameIndex(attributeName)
AttributeError: 'QgsVectorLayer' object has no attribute 'fieldNameIndex'

What´s the problem? I'm using QGIS 3.0.1 64x in windows.

10

in python 3: use

fields().lookupField() 

or fields().indexFromName()

example:

idx= layer.fields().indexFromName(attributeName)
| improve this answer | |
5

You need to access it via the QgsVectorDataProvider::fieldNameIndex method:

idx = self.layer.dataProvider().fieldNameIndex(attributeName)
| improve this answer | |
  • How do you do that in the Python console? I'm a noob. – Novvier Apr 18 '18 at 16:28
  • @MarcoUscuchagua - Click on a layer and type layer = iface.activeLayer(). Then type idx = layer.dataProvider().fieldNameIndex("fieldName"). The self` parameter is mainly used for plugins. – Joseph Apr 20 '18 at 9:15
  • The error remains. I think it is a error of QGIS and not of my layer. – Novvier Apr 20 '18 at 12:17
  • @MarcoUscuchagua - What error are you receiving? Is it the same error as in your question? – Joseph Apr 20 '18 at 12:34
  • 1
    I found the solution!! The problem was in the Quick Attribution plugin, its reinstallation solved the problem. Thanks @Joseph – Novvier Apr 24 '18 at 0:42

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.