I am writing a script to add attributes and then populate the fields. When I run the script in QGIS in the python console, it works. But when I put it in a script and run it through a plugin, it only adds the fields, but doesn't populate them.
Here is the script,
def addAtts(attName, varType):
layer = iface.activeLayer()
layer.dataProvider().addAttributes([QgsField(attName, varType)])
layer.updateFields()
attDict = {'COLOR': 176, 'ICON': None, 'L_LIMIT': 0, 'LAYER': 50, 'MAJ_CAT': 'Nautical', 'MIN_CAT': '(AREA)Lake', 'MODE': 'N', 'VALUE': None, 'U_LIMIT': 34, 'FONT': '14B', 'P_LIMIT': 0, 'P_ICON': None, 'BFR_COLOR': 0, 'BFR_WIDTH': 0, 'RTP_LIMIT': 34, 'RTP_COLOR': 43, 'WIDTH': 1, 'BDR_COLOR': 43, 'BDR_WIDTH': 1, 'IMG_RENDER': 255, 'BFR_PATTERN': None, 'PATTERN': None, 'BDR_PATTERN': None, 'FILT_SRCH': None }
layer = iface.activeLayer()
layer.startEditing()
num = 0
for x in attDict.keys():
num += 1
it_is = str(attDict[x])
try:
int(it_is)
it_is = True
#qType = 'QVariant.Int'
addAtts(x, QVariant.Int)
layer.changeAttributeValue(0,num,attDict[x])
except ValueError:
it_is = False
#qType = 'QVariant.String'
addAtts(x,QVariant.String)
layer.changeAttributeValue(0,num,attDict[x])