qgis – Fix QGIS 2.4 Python 2.7 PyQT4.Qtcore.QStringList Import Error

importpyqtqgis

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

Best Answer

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.