1

I receive an import error when trying to import QStringList from the QGIS Python console (or when loading a custom plugin).

from PyQt4.QtCore import QStringList
Traceback (most recent call last):
 File "<input>", line 1, in <module>
ImportError: cannot import name QStringList

I receive no error when using the same import from an ipython console or executing a standalone python script.

Python 2.7.5+ [GCC 4.8.1]

Please advise

2

You can't import QStringList in QGIS 2.x because we are using SIP bindings v2 which auto converts the types to Python types.

If you are buildling at a standalone app you should import qgis.core before import PyQt4 because PyQt4 will set the sip API to V1 before QGIS can set it to v2.

Long story short:

Do this:

from qgis.core import *
from PyQt4.QtCore import *

Not this:

from PyQt4.QtCore import *
from qgis.core import *

Because:

QGIS sets the API version to 2 or else PyQt4 will set it to version 1. Once it's set it can't be changed.

Version 2 is heaps better so use that.

1

Since I wasn't doing much 'exoticism' with that QStringList I tried to subsitute with a simple python List With Success.

try: from PyQt4.QtCore import QStringList except ImportError: QStringList = list

then

ql = QStringList()
l = Global.getPG_connection().tableList     
for item in l:
    ql.append(item)
    #Pass the QStringList to the QInputDialog
    table, ok = QInputDialog.getItem(self, "something", "somewhere", ql )

allow proper construction of my QInputDialog.

  • That said, I still have no clue regarding that error import! I check for name collision within my code without finding any. Anybody? – Peter Peterson Aug 6 '14 at 11:38
  • See my answer to why and what you should do. – Nathan W Aug 6 '14 at 12:13

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.