[Qgis-user] QCompleter in QGIS form

Bernhard Ströbl bernhard.stroebl at jena.de
Sun Mar 29 23:05:32 PDT 2015


Hi Stefan,

I am not answering your questions here but, if you are on a PostgreSQL 
database you might want to try out the DataDrivenInputMask plugin [1]. 
It auto-generates forms for you; ComboBoxes on lookup tables (e.g. 
species) feature auto-complete.

Bernhard

[1] http://plugins.qgis.org/plugins/DataDrivenInputMask/

Am 28.03.2015 20:32, schrieb Blumentrath, Stefan:
> Dear all,
>
> Thanks to Nathan`s excellent example here:
>
> http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/
>
> 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).
>
> 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)!
>
> Yet there are two things I am struggling with:
>
> 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 “NameError: global name 'TextEdit' is not
> defined”… Any ideas how to fix that?
>
> 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)…
>
> Many thanks in advance for helping.
>
> Kind regards,
>
> Stefan
>
> 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)) …
>
> P.P.S.: My form_init.py looks like this:
>
> from PyQt4.QtCore import *
>
> from PyQt4.QtGui import *
>
> from PyQt4.QtSql import *
>
> import psycopg2
>
> nameField = None
>
> myDialog = None
>
> def formOpen(dialog,layerid,featureid):
>
>      global myDialog
>
>      myDialog = dialog
>
>      global nameField
>
> nameField = dialog.findChild(QLineEdit,"latinsk_navn")
>
>      #Initiate completer
>
>      completer = QCompleter()
>
>      nameField.setCompleter(completer)
>
>      #Fetch data from DB to fill completer
>
>      conn = psycopg2.connect("dbname='MYDB' user='MYUSERNAME'
> host='MYHOST' password='MYPASSWD'")
>
>      cur = conn.cursor()
>
>      cur.execute("""SELECT scientificname FROM kls.l_artsliste WHERE
> finnesinorge = 'Ja'""")
>
>      completition_items = []
>
>      for row in cur.fetchall():
>
>          completition_items.append(row[0])
>
>                 #Add data to Qt Model / QCompleter
>
>                 model = QStringListModel()
>
>      model.setStringList(completition_items)
>
>      completer.setModel(model)
>
>


__________ Information from ESET Mail Security, version of virus signature database 11396 (20150330) __________

The message was checked by ESET Mail Security.
http://www.eset.com





More information about the Qgis-user mailing list