1

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)
        

enter image description here

1 Answer 1

1

You can just create a conversion dict to convert from text (or the index) of the combobox to the QVariant equivalent


class X:

    CONVERSION_TYPE = {
        "Int": QVariant.Int,
        "Double": QVariant.Double
        "String": QVariant.String
    }


    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()
   
        # Get the QVariant type of the combobox text
        field_type = self.CONVERSION_TYPE[self.dlg.cb_field_type.currentText()]
    
    
    
        #here in QgsField , i wanna 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:
            # Create the field with the correct type
            res = layer.dataProvider().addAttributes([QgsField(newF, field_type)])
            layer.updateFields()

    def run(self):
        #this is how I am populating the combobox for the data type of the field
        fieldType_list = list(self.CONVERSION.keys())
        self.dlg.cb_field_type.addItems(fieldType_list)
4
  • Thank you , it totally works.
    – user211028
    Commented Sep 8, 2022 at 6:34
  • I just wanna ask one more thing , here when i try to print field type , it says 2 for int , 6 for double, and10 for string why so?
    – user211028
    Commented Sep 8, 2022 at 6:35
  • 1
    Qvariant.Int and the others are variables containing intergers because as far as I know they either are enumerations or behave like it. Its a quite common practice to have clear variable names hiding integers/strings/... for ease of use
    – Kalak
    Commented Sep 8, 2022 at 6:58
  • okay got it, thank you
    – user211028
    Commented Sep 8, 2022 at 7:00

Your Answer

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