2

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

1 Answer 1

1

First, I tried your code out in Python Console of QGIS 3.8, with layer of following image, and it adds and populates the fields in attributes table.

enter image description here

Afterwards, I created a test plugin and put your code in it (slightly modified for working through plugin) with two functions before run method: addAtts and adding_attributes (this one connected to OK button). Code Snippet looks as follow:

.
.
.
from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, QVariant
.
.
.
from qgis.core import QgsField
.
.
.
    def add_action(
.
.
.
        okBtn = self.dlg.okButton
        okBtn.clicked.connect(self.adding_attributes)

        return action
.
.
.
    def addAtts(self, attName, varType):
        layer = self.iface.activeLayer()
        layer.dataProvider().addAttributes([QgsField(attName, varType)])
        layer.updateFields()

    def adding_attributes(self):

        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 = self.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'
                self.addAtts(x, QVariant.Int)
                layer.changeAttributeValue(0,num,attDict[x])

            except ValueError:
                it_is = False
                #qType = 'QVariant.String'
                self.addAtts(x,QVariant.String)
                layer.changeAttributeValue(0,num,attDict[x])

        layer.commitChanges()        

    def run(self):
.
.
.

After reloading plugin to save changes, I erased all layer fields created with script in Python Console and ran the plugin. Result was the same as in above image.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.