1

I have made a Dialog in PyQt for use it like a plugin under QGIS 2.10

The Dialog works fine like a standalone application, but if I try to call it like a QGIS plugin, I get this error:

AttributeError: 'module' object has no attribute 'QStringList'

There are a similar question here, but it doesn't work for me.

The form is made with Qt Designer

This is a snippet of my Dialog:

from qgis.core import *
from PyQt4 import QtCore, QtGui, QtSql

from Ui_BuscadorParcelas import Ui_BuscadorParcelas

# create the dialog for BuscadorParcelas
class BuscadorParcelasDialog(QtGui.QDialog):
    def __init__(self):
    .......................
    def PrepararCuadros(self):
        self.consulta = QtSql.QSqlQuery(self.conexion)      
        if self.consulta:
            for i in range(self.numCamposLineEdit):
                cadenaConsulta = "SELECT DISTINCT "+self.campo[i]+" FROM crcc ORDER BY "+self.campo[i];
                self.consulta.exec_(cadenaConsulta)
                self.ListaValores = QtCore.QStringList()
         ...................................................

I need the QStringList() to set a QCompleter object

I complete the answer of Mathias Kuhn saying that if I get the values of the list from a database query, like my example, I need to prepend unicode for obtain a python list that QCompleter can use like argument (first line in the following code replace last line of first one)

self.ListaValores = []
while self.consulta.next():
    self.ListaValores.append(unicode(self.consulta.value(0)))
self.completer = QtGui.QCompleter(self.ListaValores, self)
2

QGIS updated to the SIP API V2 between 1.8 and 2.0.

See: https://hub.qgis.org/wiki/17/Python_plugin_API_changes_from_18_to_20#Replace-QStringList-with-list

It will transparently convert some types (like QString or QList) between python and Qt types.

So you can just use a python list of str or unicode objects and pass it to the pyqgis and pyqt API wherever a QStringList is expected.

listaValores = [u'uno', u'dos', u'tres']
completer = QCompleter(listaValores)
  • I have followed your instructions but Completer complaints about the type – user3733164 Jul 25 '15 at 13:07
  • Well, I have made an example out, with a standalone QLineEdit, and it works! Complete accept a python list like argument. But inside QGIS doesn accept a python list like argument – user3733164 Jul 25 '15 at 15:29
  • Thank you. Now it works. I have made a small remark to your answer – user3733164 Jul 26 '15 at 14:37
  • I guess that applies if the values you get are not of type str or unicode (like integers, doubles...) then you will have to convert them so you get a QStringList instead of a QVariantList – Matthias Kuhn Jul 26 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.