I am trying to add a field using plugin in QGIS 3. I just want the new field to be of specific data type, so I have created a combo box and populated it with the QVariant types i.e string, double, int,...
Here is my code -
def addfield(self):
layer = self.dlg.cb_layer.currentLayer()
#newF is basically the lineedit where user enters the new field name
newF = self.dlg.new_field.text()
field_type = self.dlg.cb_field_type.currentText()
# Here in QgsField, I want to know how to take the input of what should be the data type of new field , so I have give the combo box in the plugin,as seen in the picture attached below
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
res = layer.dataProvider().addAttributes([QgsField(newF,QVariant.)])
layer.updateFields()
def run(self):
#this is how I am populating the combobox for the data type of the field
fieldType_list = ["String", "Int","Double"]
self.dlg.cb_field_type.addItems(fieldType_list)