I'm constructing new memory layer to do some spatial data processing. I opened new QgsVectorLayer and I'm trying to add fields dynamically using

fld = QgsField("Name", typeName = "TypeName")

Constructing a list of fields with a loop based on information from other layers.

When I call:

layer.dataProvider().addAttributes([list of QgsField])    

and then to check :

print("Layer init fields:", [field.name() for field in layer.fields()])

I get empty list, which means there are no initialized attributes in a layer.

My question is QgsField correctly initialized?

res = layer.dataProvider().addAttributes(field_def_list)
print("Adding attributes...:",res)

Says it is initialized. Any idea where I abused the QgsField init. Is initializing QgsField by typeName right way to go defining new fields or should I use QVariant.Type?


1 Answer 1


So to answer my own question, QVariant.Type has to be defined if you want to construct QgsField. Defining typeName doesn't work.

Your Answer

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

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