[Qgis-user] QGIS custom feature forms with python logic - passed in feature

Martin Bain Martin.Bain at lismore.nsw.gov.au
Mon Mar 27 19:25:12 PDT 2017


Hi Jen,
Its funny, I happened to be playing around with the same sample code from NathanW when I saw your post.  I meant to reply earlier but got distracted.  I was able to change an attribute of the passed in feature with the modified sample below.  (In my case I was curious about dynamically adding controls to the form at runtime and getting them to display attribute data.  The upshot of that was the data in the dynamically added control was not automatically saved (which is understandable), I needed to add code in the validate() routine to write the data.


from PyQt4.QtCore import *
from PyQt4.QtGui import *

nameField = None
myDialog = None

def formOpen(dialog,layerid,featureid):
    global myDialog
    myDialog = dialog
    global nameField

    #store a reference to the selected layer
    global lyr
    lyr=layerid

    #store a reference to the selected feature
    global fid
    fid=featureid

    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")

    # Disconnect the signal that QGIS has wired up for the dialog to the button box.
    buttonBox.accepted.disconnect(myDialog.accept)

    # Wire up our own signals.
    buttonBox.accepted.connect(validate)
    buttonBox.rejected.connect(myDialog.reject)

    #dynamically add a control to the form at runtime and populate with the value of the "Road_Name" field
    nameField = QtGui.QLineEdit(myDialog)
    nameField.setObjectName("Road_Name")
    myDialog.layout().addWidget(nameField)
    nameField.show()
    nameField.setText(featureid["Road_Name"])

def validate():
  # Make sure that the name field isn't empty.
    if not len(nameField.text()) > 0:
        msgBox = QMessageBox()
        msgBox.setText("Name field can not be null.")
        msgBox.exec_()
    else:
        # Return the form as accpeted to QGIS.
        myDialog.accept()

        #update the value of the "Road_Name" field for the selected feature
        fid["Road_Name"]=nameField.text()
        lyr.updateFeature(fid)

        #Call commit to save the changes
        #layer.commitChanges()



From: Qgis-user [mailto:qgis-user-bounces at lists.osgeo.org] On Behalf Of Jen Swiger
Sent: Thursday, 16 March 2017 8:00 AM
To: qgis-user at lists.osgeo.org
Subject: [Qgis-user] QGIS custom feature forms with python logic - passed in feature

Hello,
I hope I am asking this question in the right email list. I may ask in the qgis stack exchange as well.
I am working off this example https://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/ and adjusting to meet my needs.
When the formOpen function is called, the dialog, layer, and feature is passed in.
I want to edit the feature that is passed in but code like
mylayer.changeAttribute(myfeature.id<http://myfeature.id>(), 10, 1)
doesn't seem to be working because myfeature.id<http://myfeature.id>() returns 0.
What code should I be using instead to edit a field in the feature?
Thanks!
Jen

This email is intended for the named recipient only.  The information it contains may be confidential.  If you are not the intended recipient you must not reproduce or distribute any part of this email, disclose its contents to any other party or take any action in reliance on it.  If you have received this email in error, please contact the sender immediately and delete the message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20170328/2b7b1a55/attachment.html>


More information about the Qgis-user mailing list