1

I want to update some fields from a selected feature in an edit session with some values provided by a form made in QtDesigner. The fields have indexes 0 and 2.

Tried so far:

from edit_Form import Ui_EditForm

class editForm(QtGui.QDialog):
 def __init__(self):
    QtGui.QDialog.__init__(self)
    self.ui = Ui_EditForm()
    self.ui.setupUi(self)
    self.ui.pushButton.clicked.connect(self.accept)

 def accept(self):

     for i in iface.activeLayer().selectedFeatures():
        fid = i.id()
        provider = iface.activeLayer().dataProvider()
        name = iface.activeLayer().fieldNameIndex("NAME")
        siruta = iface.activeLayer().fieldNameIndex("SIRUTA")
        textNAME = self.ui.NAME.text()
        textSIRUTA = self.ui.SIRUTA.text()
        i.setAttribute(0,self.ui.NAME.text() )
        i.setAttribute(2,self.ui.SIRUTA.text())

2 Answers 2

1

I found the solution:

def accept(self):

 for i in iface.activeLayer().selectedFeatures():
    fid = i.id()
    provider = iface.activeLayer().dataProvider()
    name = iface.activeLayer().fieldNameIndex("NAME")
    siruta = iface.activeLayer().fieldNameIndex("SIRUTA")
    textNAME = self.ui.NAME.text()
    textSIRUTA = self.ui.SIRUTA.text()
    iface.activeLayer().changeAttributeValue(fid,name,textNAME)
    iface.activeLayer().changeAttributeValue(fid,siruta,textSIRUTA)
    iface.activeLayer().commitChanges()
0

If you use QGIS 2.0+ then:

def accept(self):
    layer = iface.activeLayer()
    layer.startEditing()
    textNAME = self.ui.NAME.text()
    textSIRUTA = self.ui.SIRUTA.text()
    for i in layer.selectedFeatures():
        i["NAME"] = textNAME
        i["SIRUTA"] = textSIRUTA
    layer.commitChanges()
2
  • I use Qgis 2.12.3 Lyon, i run the loop in Python Console, but no result. After running: iface.activeLayer().commitChanges(), I get "True" bollean, without no effects in attribute table.
    – G3orge
    Commented Feb 19, 2016 at 10:37
  • I forget to change layer to your situation. I've edited my answer
    – Zoltan
    Commented Feb 19, 2016 at 11:04

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.