2

I set up a QgsFieldComboBox for a Plugin. I know I can read the selected fieldname the following:

self.dlg.MyQgsFieldComboBox.currentField()

Studying the docs, I cannot find an option to read the currentFieldType() directly. So I know I can get field types of my selected layer the following:

self.mylayerfieldtypes = [field.typeName() for field in self.myselectedlayer.fields()]

I could stitch this together the following, to get the field type only of my selected field:

myfieldname = self.dlg.MyQgsFieldComboBox.currentField()

for field in self.myselectedlayer.fields():
   if field.name() == myfieldname:
       myfieldtype = field.typeName()

But my issue now is, when creating a new layer having a field with the same type as my selected field, I cannot just use myfieldtype for it like

QgsField("MyNewFieldHavingTheSameTypeAsMyOldField", myfieldtype)

How can I create a new field, having the same type as my selected field? Copying the field to my new layer is not an option.

1 Answer 1

2

Sometimes the answer is just too simple: instead of field.typeName() just use field.type().

myfieldname = self.dlg.MyQgsFieldComboBox.currentField()

for field in self.myselectedlayer.fields():
   if field.name() == myfieldname:
       myfieldtype = field.type()
QgsField("MyNewFieldHavingTheSameTypeAsMyOldField", myfieldtype)

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.