Skip to main content
added 232 characters in body
Source Link
Kalak
  • 4k
  • 12
  • 28

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)

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()

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)
Source Link
Kalak
  • 4k
  • 12
  • 28

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()