1

I'm trying to implement the code below to run when adding a feature in the layer, but nothing happens even an error message.

However when I call the method directly, the routing is executed normally.

Any suggestions for the problem?

from qgis.core import *
from qgis.utils import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt.QtWidgets import QWidget

def Form(dialog, layer, feature):
    layer.featureAdded.connect(lambda: fillFields(dialog, layer, layer.dataProvider(), 'Id', 'Tag', "CE-C"))

def fillFields(Dialog, Layer, Provider, FieldId, FieldTag, LabelTag):
    Layer.startEditing()
    lineEdit_tag = Dialog.findChild(QLineEdit, FieldTag)
    id = Provider.fieldNameIndex(FieldId)
    tag = Provider.fieldNameIndex(FieldTag)
    num = Provider.featureCount()
    lineEdit_tag.setText(LabelTag + str(num + 1))
    list = range(num)
    i = 0
    j = 1
    for item in list:
        new_values = {id: j, tag: LabelTag + str(j)}
        Provider.changeAttributeValues({i: new_values})
        i += 1
        j += 1
    Layer.commitChanges()
  • In old code (that works) from 2011 I've got self.connect(layer,SIGNAL("featureAdded(int)"),self.AddedFeat) where AddedFeat is a procedure similar to fillFields. Not sure if that's still valid for the current version. – Michael Stimson Nov 13 '17 at 1:45
  • I tested the following code and it did not work from qgis.core import * from qgis.utils import * from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.PyQt.QtWidgets import QWidget def UpdateColorForm(dialog, layer, feature): dialog.connect(layer, SIGNAL("featureAdded(int)"), MessagemBox) def MessagemBox(): some_variable = feature.geometry() QMessageBox.information(None, "DEBUG:", str(some_variable)) – Marcelo Nov 15 '17 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.