<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    PS:<br>
    <br>
    1) couldn't reproduce the "global name is not defined" error... You
    script works here with .ui and autogenerated forms.<br>
    2) You may use a global variable to cache your completer (or its
    values)<br>
    <br>
    Best,<br>
    Matthias<br>
    <br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 03/28/2015 08:32 PM, Blumentrath,
      Stefan wrote:<br>
    </div>
    <blockquote
      cite="mid:f3dff30863d74f67bcde5e5ecdd2ba59@NINSRV23.nina.no"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <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:"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;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1784958659;
        mso-list-type:hybrid;
        mso-list-template-ids:874516228 68419601 68419609 68419611 68419599 68419609 68419611 68419599 68419609 68419611;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></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 lang="EN-US">Dear all,<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">Thanks to Nathan`s
            excellent example here:<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><a
              moz-do-not-send="true"
href="http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/">http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/</a><o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">I managed to implement a
            simple QCompleter for a LineEdit in my form where the
            completer items are being fetched from DB (see init function
            code at the end of my mail).<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">This is very, very
            promising stuff which comes in quite handy for entering
            species names (from an official list of (thousands of)
            species names in my case)!<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">Yet there are two things
            I am struggling with:<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">1) Would it be possible
            to use this QCompleter solution to an autogenerated form?
            The auto generated forms are more than good enough for most
            of my use-cases. When I tried the Init function on an
            autogenerated form I got “</span><span
            style="font-family:"Courier New,courier",serif"
            lang="EN-US">NameError: global name 'TextEdit' is not
            defined”… Any ideas how to fix that?</span><span
            lang="EN-US"><o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">2) It seems to me that
            the query - which is used to fill the QCompleter - is
            executed each time the form is opened. Would it be possible
            to avoid this, I mean that the query is only run once (e.g.
            when I open the project)? I hoped this could speed up the
            start of the form (even if the query gets cached in
            PostgreSQL)…<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">Many thanks in advance
            for helping.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">Kind regards,<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">Stefan<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">P.S.: Next thing I will
            have to do is understand SIGNALS and SLOTS so I can adjust
            the completer content depending on other data entered in the
            form (e.g. filter species by kingdom or known occurrence in
            a country (here Norway)) …<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">P.P.S.: My form_init.py
            looks like this:<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">from PyQt4.QtCore import
            *<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">from PyQt4.QtGui import
            *<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">from PyQt4.QtSql import
            *<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">import psycopg2<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">nameField = None<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">myDialog = None<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">def
            formOpen(dialog,layerid,featureid):<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">    global myDialog<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">    myDialog = dialog<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">    global nameField<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="NO-NYN">    </span><span
            lang="EN-US">nameField =
            dialog.findChild(QLineEdit,"latinsk_navn")<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    <o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    #Initiate completer<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    completer =
            QCompleter()<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">   
            nameField.setCompleter(completer)<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    <o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    #Fetch data from DB
            to fill completer<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    conn =
            psycopg2.connect("dbname='MYDB' user='MYUSERNAME'
            host='MYHOST' password='MYPASSWD'")<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    cur = conn.cursor()<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">   
            cur.execute("""SELECT scientificname FROM kls.l_artsliste
            WHERE finnesinorge = 'Ja'""")<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    completition_items =
            []<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">    for row in
            cur.fetchall():<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">       
            completition_items.append(row[0])<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">               #Add data
            to Qt Model / QCompleter<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">               model =
            QStringListModel()<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">   
            model.setStringList(completition_items)<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">   
            completer.setModel(model)<o:p></o:p></span></p>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Qgis-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/qgis-user">http://lists.osgeo.org/mailman/listinfo/qgis-user</a></pre>
    </blockquote>
    <br>
  </body>
</html>