I am new to pyqgis plugins with QT, I am writing code to replace multiple headers in the attribute field of a layer. I keep getting an error saying
'int' object has no attribute 'pendingFields'
What can I do?
Here is my code. The code accepts the current shapefile and renames the headers:
def run(self):
"""Run method that performs all the real work"""
# show the dialog
#This activates the layer on the table of content
self.dlg.layercombo.clear()
layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
layerType = layer.type()
if layerType == QgsMapLayer.VectorLayer:
layer_list.append(layer.name())
self.dlg.layercombo.addItems(layer_list)
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
self.selectedlayerIndex = self.dlg.layercombo.currentIndex()
for field in self.selectedlayerIndex.pendingFields():
if field.name() == 'CC':
with edit(self.selectedlayerIndex):
idx = self.selectedlayerIndex.fieldNameIndex(field.name())
self.selectedlayerIndex.renameAttribute(idx, 'DD')
if field.name() == 'AA':
with edit(self.selectedlayerIndex):
idx = self.selectedlayerIndex.fieldNameIndex(field.name())
self.selectedlayerIndex.renameAttribute(idx, 'BB')
self.dlg.layercombo.currentIndex()
return an integer (the index of a layer in your combo), and not the layer itself, and the pendingFields method works on Layer Object.