<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-AU" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Hi Jen,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">from</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> PyQt4.QtCore </span><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">import</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> *<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">from</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> PyQt4.QtGui </span><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">import</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> *<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">nameField = </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">None</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">myDialog = </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">None</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">def</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> </span><span style="font-size:10.5pt;font-family:Consolas;color:#DCDCAA">formOpen</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">(</span><span style="font-size:10.5pt;font-family:Consolas;color:#9CDCFE">dialog</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">,</span><span style="font-size:10.5pt;font-family:Consolas;color:#9CDCFE">layerid</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">,</span><span style="font-size:10.5pt;font-family:Consolas;color:#9CDCFE">featureid</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">):<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">global</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> myDialog<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    myDialog = dialog<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">global</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> nameField<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#store a reference to the selected layer</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">global</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> lyr<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    lyr=layerid<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#store a reference to the selected feature</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">global</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> fid<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    fid=featureid<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    buttonBox = dialog.findChild(QDialogButtonBox,</span><span style="font-size:10.5pt;font-family:Consolas;color:#CE9178">"buttonBox"</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E"># Disconnect the signal that QGIS has wired up for the dialog to the button box.</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    buttonBox.accepted.disconnect(myDialog.accept)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E"># Wire up our own signals.</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    buttonBox.accepted.connect(validate)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    buttonBox.rejected.connect(myDialog.reject)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#dynamically add a control to the form at runtime and populate with the value of the "Road_Name" field</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    nameField = QtGui.QLineEdit(myDialog)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    nameField.setObjectName(</span><span style="font-size:10.5pt;font-family:Consolas;color:#CE9178">"Road_Name"</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    myDialog.layout().addWidget(nameField)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    nameField.show()<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    nameField.setText(featureid[</span><span style="font-size:10.5pt;font-family:Consolas;color:#CE9178">"Road_Name"</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">])<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#569CD6">def</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> </span><span style="font-size:10.5pt;font-family:Consolas;color:#DCDCAA">validate</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">():<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">  </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E"># Make sure that the name field isn't empty.</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">if</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"> not </span><span style="font-size:10.5pt;font-family:Consolas;color:#DCDCAA">len</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">(nameField.text()) > </span><span style="font-size:10.5pt;font-family:Consolas;color:#B5CEA8">0</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        msgBox = QMessageBox()<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        msgBox.setText(</span><span style="font-size:10.5pt;font-family:Consolas;color:#CE9178">"Name field can not be null."</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        msgBox.exec_()<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">    </span><span style="font-size:10.5pt;font-family:Consolas;color:#C586C0">else</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E"># Return the form as accpeted to QGIS.</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        myDialog.accept()<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#update the value of the "Road_Name" field
 for the selected feature</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        fid[</span><span style="font-size:10.5pt;font-family:Consolas;color:#CE9178">"Road_Name"</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">]=nameField.text()<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        lyr.updateFeature(fid)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#Call commit to save the changes</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4">        </span><span style="font-size:10.5pt;font-family:Consolas;color:#608B4E">#layer.commitChanges()</span><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#1E1E1E"><span style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Qgis-user [mailto:qgis-user-bounces@lists.osgeo.org]
<b>On Behalf Of </b>Jen Swiger<br>
<b>Sent:</b> Thursday, 16 March 2017 8:00 AM<br>
<b>To:</b> qgis-user@lists.osgeo.org<br>
<b>Subject:</b> [Qgis-user] QGIS custom feature forms with python logic - passed in feature<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Hello,<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">I hope I am asking this question in the right email list. I may ask in the qgis stack exchange as well.<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">I am working off this example <a href="https://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/">
https://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/</a> and adjusting to meet my needs.
<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">When the formOpen function is called, the dialog, layer, and feature is passed in.
<o:p></o:p></p>
</div>
<p class="MsoNormal">I want to edit the feature that is passed in but code like<o:p></o:p></p>
</div>
<p class="MsoNormal"><span style="font-family:"Courier New"">mylayer.changeAttribute(<a href="http://myfeature.id">myfeature.id</a>(), 10, 1)</span><o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">doesn't seem to be working because
<span style="font-family:"Courier New""><a href="http://myfeature.id">myfeature.id</a>()
</span><span style="font-family:"Arial",sans-serif">returns 0. </span><o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-family:"Arial",sans-serif">What code should I be using instead to edit a field in the feature?</span><o:p></o:p></p>
</div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif">Thanks!</span><o:p></o:p></p>
</div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif">Jen</span><o:p></o:p></p>
</div>
</div>
<br clear="both">
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.<BR>
</body>
</html>