1

I am trying to write a function to delete a field using plugin in QGIS 3.24. I have build a plugin , which consists of QgsFieldComboBox. So I can select a field from the combo box to delete (from the layer selected), take it as input and delete it. Here is the plugin -

enter image description here

Here is the code -

def delete_field(self): layer = self.dlg.cb_layer.currentLayer()

#HERE WE ARE TAKING THE INPUT OF FIELD SELECTED IN COMBOBOX #delField_cb is the object of the QgsFieldComboBox

    delF = self.dlg.delField_cb.currentField()
    print(delF)
   
    
    
    
    caps = layer.dataProvider().capabilities()
    if caps & QgsVectorDataProvider.DeleteAttributes:
        res = layer.dataProvider().deleteAttributes([delF])
        layer.updateFields()

def delete_field_Cb(self,vlayer): self.dlg.delField_cb.setLayer(vlayer) field = self.dlg.delField_cb.setLayer(vlayer)

def run(self):

   self.dlg.cb_layer.layerChanged.connect(self.delete_field_Cb)
   self.dlg.delete_field_btn.clicked.connect(self.delete_field_Cb)

I am stuck and gettting the error as:

field = self.dlg.delField_cb.setLayer(vlayer) TypeError: QgsFieldComboBox.setLayer(): argument 1 has unexpected type 'bool'

2 Answers 2

0

self.dlg.delete_field_btn.clicked.connect(self.delete_field_Cb) this causes your error because when the clicked event send a boolean to the called function

What you want is most likely self.dlg.delete_field_btn.clicked.connect(self.delete_field)

Also in your delField_cb function: field = self.dlg.delField_cb.setLayer(vlayer) doesn't seem to do anything

EDIT: for the second exception replace res = layer.dataProvider().deleteAttributes([delF]) with

index_field = layer.fields().indexFromName(delF)
res = layer.dataProvider().deleteAttributes([index_field])
3
  • Now i am getting this as an error SystemError: returned a result with an error set TypeError: an integer is required (got type str) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\abtdmodel\ABTDModel.py", line 288, in delete_field res = layer.dataProvider().deleteAttributes([delF]) SystemError: returned a result with an error set
    – user211028
    Commented Sep 8, 2022 at 10:18
  • updated my answer
    – Kalak
    Commented Sep 8, 2022 at 10:25
  • yES GOT IT , THANK YOU.
    – user211028
    Commented Sep 8, 2022 at 10:40
0

I got the answer to my Error. I had to put in the index of the field which i want to delete and not the field name in

res = layer.dataProvider().deleteAttributes([*delF]*)

so to get the index we write the code as:

def delete_field(self):
        layer = self.dlg.cb_layer.currentLayer()
        delF = self.dlg.delField_cb.currentField()
      # THIS TAKES THE INDEX OF THE SELECTED FIELD FROM COMBO BOX
        idx = layer.dataProvider().fieldNameIndex(delF)

        caps = layer.dataProvider().capabilities()
        if caps & QgsVectorDataProvider.DeleteAttributes:
            res = layer.dataProvider().deleteAttributes([idx])
            layer.updateFields()
            

Your Answer

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