<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Hi Tom,<br>
    <br>
    Hmm... Maybe there is such a method do determine the context (can't
    recall right now). But in your case I would simply use something
    like:<br>
    <br>
    geom = ...<br>
    parishField = ...<br>
    if not geom:<br>
        parishField.setPlaceholderText('No geometry found')<br>
        return<br>
    <br>
    <div class="moz-cite-prefix">On 05/03/2016 04:49 PM, Thomas Colley
      wrote:<br>
    </div>
    <blockquote
cite="mid:C6BE00E0E4E1A547B4E1E360B18256D7BF8AE984@NPT-EXCH2010P1.neath-porttalbot.gov.uk"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta name="Generator" content="Microsoft Word 14 (filtered
        medium)">
      <style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
        {font-family:"Courier New\,courier";
        panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        color:black;
        mso-fareast-language:EN-US;}
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;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";
        color:black;
        mso-fareast-language:EN-US;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;
        color:black;
        mso-fareast-language:EN-US;}
span.EmailStyle19
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.EmailStyle20
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";
        color:black;
        mso-fareast-language:EN-US;}
span.EmailStyle23
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@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]-->
      <div class="WordSection1">
        <p class="MsoNormal"><span style="color:#1F497D">Hi Matthias<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">Thanks for the
            explanation, I think I am using globals where I shouldn’t
            be. In this case I don’t think it’s the global variable
            causing the problem though.<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">I’ve stripped
            everything back to a really simple example (no global
            variables). I have a custom UI with one QLineEdit called
            parish.
            <o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">On opening the
            form Python performs an intersect with the feature against a
            polygon layer of parishes and uses the parish name to set
            the text in the QLineEdit.<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">from
            PyQt4.QtCore import *<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">from
            PyQt4.QtGui import *<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">from qgis.core
            import *<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">from qgis.gui
            import *<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">def
            my_form_open(dialog, layer, feature):<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">               
            geom = feature.geometry()<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">               
            parishField = dialog.findChild(QLineEdit, "parish")<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">               
            for lyr in
            QgsMapLayerRegistry.instance().mapLayers().values():<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                               
            if lyr.name() == 'parishes':<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                                               
            for f in lyr.getFeatures():<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                                                               
            if geom.intersects(f.geometry()):<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                                                                               
            parishField.setText(f.attribute('name'))<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                                                                               
            break<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">                                               
            break<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">This works fine
            using the identify tool but throws up the following error
            when opening the attribute table:<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Times New
            Roman","serif";color:red;mso-fareast-language:EN-GB">An
            error occurred during execution of following code:<br>
          </span><span style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:red;mso-fareast-language:EN-GB">my_form_open(
            _qgis_featureform_22_16,
            _qgis_layer_test_lines20160503150512486_16,
            _qgis_feature_20160503154237941)</span><span
            style="font-size:12.0pt;font-family:"Times New
Roman","serif";color:windowtext;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB"><br>
            Traceback (most recent call last):<br>
              File "", line 1, in <br>
              File "", line 23, in my_form_open<br>
            AttributeError: 'NoneType' object has no attribute
            'intersects'<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB">I
            can see why the error is happening: When opening the
            attribute table the feature is passed without geometry so
            the intersect fails, but for my purposes I don’t want it to
            be running the Python code anyway as I don’t want it
            calculating fields unless I’ve actually clicked on the
            feature. Opening the attribute table just runs it against
            the first record in the table.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB">That’s
            why I was hoping there’d be some way of detecting the
            context of the form. QgsAttributeEditorContext has a
            FormMode that returns Embed, StandaloneDialog or Popup, so I
            thought that looked promising but I can’t work out how to
            access it.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Courier
New,courier","serif";color:windowtext;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">Thanks<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D">Tom<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
        <div>
          <div style="border:none;border-top:solid #B5C4DF
            1.0pt;padding:3.0pt 0cm 0cm 0cm">
            <p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext;mso-fareast-language:EN-GB"
                  lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext;mso-fareast-language:EN-GB"
                lang="EN-US"> Matthias Kuhn [<a class="moz-txt-link-freetext" href="mailto:matthias@opengis.ch">mailto:matthias@opengis.ch</a>]
                <br>
                <b>Sent:</b> 03 May 2016 13:52<br>
                <b>To:</b> Thomas Colley; <a class="moz-txt-link-abbreviated" href="mailto:qgis-user@lists.osgeo.org">qgis-user@lists.osgeo.org</a><br>
                <b>Subject:</b> Re: [Qgis-user] Python form logic on
                attribute table open<o:p></o:p></span></p>
          </div>
        </div>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal" style="margin-bottom:12.0pt">Hi Tom,<br>
          <br>
          Globals are really for "there will only ever be one of these"
          objects. And that's not true for feature forms.<br>
          You will have the same kind of trouble if the user opens the
          feature form of two features in parallel because they will
          work on the same global variable (which will be overwritten
          and may even be deleted by form A whil form B is still open).<br>
          <br>
          I assume you have a slot connected to some widget's changed
          signal and need the reference to parameters inside the slot.
          It's best to pass them as parameters to the slot.<br>
          <br>
          Something like:<br>
          <br>
          ----------<br>
          <br>
          import functools<br>
          <br>
          def my_slot( my_form, new_text ):<br>
              do_something_with(my_control)<br>
              print new_text<br>
          <br>
          def form_init(form, y, z):<br>
              control.textChanged.connect(functools.partial(my_slot,
          form))<br>
          <br>
          ----------<o:p></o:p></p>
        <div>
          <p class="MsoNormal">On 05/03/2016 01:50 PM, Thomas Colley
            wrote:<o:p></o:p></p>
        </div>
        <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
          <p class="MsoNormal"><span style="color:#1F497D">Hi Matthias</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">I am using
              global variables. In what way do they cause problems?</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">Is it
              expected behaviour for the Python logic to be used when
              the attribute table is opened?</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">I know what
              is causing the Python error. I am performing an intersect
              query to populate an attribute. When I open the attribute
              table the first feature in the attribute table is being
              passed to the Python form object but it doesn’t include
              the geometry. This gives </span><span
              style="font-family:"Courier New"">AttributeError:
              'NoneType' object has no attribute 'intersects'.</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="font-family:"Courier
              New""> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">I have no
              need for any of the Python logic on opening of the
              attribute table, although I can see the advantage if it
              was open in form view.</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">Hope this
              makes sense, if you have any more details on the global
              variables I’d be grateful.</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D">Thanks</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"><br>
              Tom</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="color:#1F497D"> </span><o:p></o:p></p>
          <div>
            <div style="border:none;border-top:solid #B5C4DF
              1.0pt;padding:3.0pt 0cm 0cm 0cm">
              <p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext;mso-fareast-language:EN-GB"
                    lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext;mso-fareast-language:EN-GB"
                  lang="EN-US"> Qgis-user [<a moz-do-not-send="true"
                    href="mailto:qgis-user-bounces@lists.osgeo.org">mailto:qgis-user-bounces@lists.osgeo.org</a>]
                  <b>On Behalf Of </b>Matthias Kuhn<br>
                  <b>Sent:</b> 03 May 2016 09:45<br>
                  <b>To:</b> <a moz-do-not-send="true"
                    href="mailto:qgis-user@lists.osgeo.org">qgis-user@lists.osgeo.org</a><br>
                  <b>Subject:</b> Re: [Qgis-user] Python form logic on
                  attribute table open</span><o:p></o:p></p>
            </div>
          </div>
          <p class="MsoNormal"> <o:p></o:p></p>
          <p class="MsoNormal" style="margin-bottom:12.0pt">Hi Tom,<br>
            <br>
            Are you using global variables? They often lead to troubles
            in this context.<br>
            <br>
            Matthias<o:p></o:p></p>
          <div>
            <p class="MsoNormal">On 05/03/2016 10:42 AM, Thomas Colley
              wrote:<o:p></o:p></p>
          </div>
          <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
            <p class="MsoNormal">Hi<o:p></o:p></p>
            <p class="MsoNormal"> <o:p></o:p></p>
            <p class="MsoNormal">I have a Python init function bound to
              an attribute form. This is working great using the
              identify tool to open a feature form but is throwing an
              error when I open the layer’s attribute table.<o:p></o:p></p>
            <p class="MsoNormal"> <o:p></o:p></p>
            <p class="MsoNormal">Is there any way I can check in the
              Python whether a feature form has been opened or the
              attribute table. I have no need for the logic when opening
              the attribute table.<o:p></o:p></p>
            <p class="MsoNormal"> <o:p></o:p></p>
            <p class="MsoNormal">I’ve found QgsAttributeEditorContext
              that looks like it might do the trick but I can’t work out
              how to access it.<o:p></o:p></p>
            <p class="MsoNormal"> <o:p></o:p></p>
            <p class="MsoNormal">Any advice would be appreciated.<o:p></o:p></p>
            <p class="MsoNormal"> <o:p></o:p></p>
            <p class="MsoNormal">Thanks<o:p></o:p></p>
            <p class="MsoNormal"><br>
              Tom<o:p></o:p></p>
            <p class="MsoNormal"><span style="font-size:12.0pt"><br>
                <br>
                <br>
                <br>
              </span><o:p></o:p></p>
            <pre>_______________________________________________<o:p></o:p></pre>
            <pre>Qgis-user mailing list<o:p></o:p></pre>
            <pre><a moz-do-not-send="true" href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a><o:p></o:p></pre>
            <pre>List info: <a moz-do-not-send="true" href="http://lists.osgeo.org/mailman/listinfo/qgis-user">http://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre>
            <pre>Unsubscribe: <a moz-do-not-send="true" href="http://lists.osgeo.org/mailman/listinfo/qgis-user">http://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre>
          </blockquote>
          <p class="MsoNormal"><span style="font-size:12.0pt"><br>
              <br>
              <br>
            </span><o:p></o:p></p>
          <pre>-- <o:p></o:p></pre>
          <pre>Matthias Kuhn<o:p></o:p></pre>
          <pre>OPENGIS.ch - <a moz-do-not-send="true" href="https://www.opengis.ch">https://www.opengis.ch</a><o:p></o:p></pre>
          <pre>Spatial • (Q)GIS • PostGIS • Open Source<o:p></o:p></pre>
        </blockquote>
        <p class="MsoNormal"><span
            style="font-size:12.0pt;font-family:"Times New
            Roman","serif";mso-fareast-language:EN-GB"><br>
            <br>
            <o:p></o:p></span></p>
        <pre>-- <o:p></o:p></pre>
        <pre>Matthias Kuhn<o:p></o:p></pre>
        <pre>OPENGIS.ch - <a moz-do-not-send="true" href="https://www.opengis.ch">https://www.opengis.ch</a><o:p></o:p></pre>
        <pre>Spatial • (Q)GIS • PostGIS • Open Source<o:p></o:p></pre>
      </div>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Matthias Kuhn
OPENGIS.ch - <a class="moz-txt-link-freetext" href="https://www.opengis.ch">https://www.opengis.ch</a>
Spatial • (Q)GIS • PostGIS • Open Source</pre>
  </body>
</html>