Sunday 31 December 2017

QGIS 2.4 - Python 2.7 PyQT4.Qtcore.QStringList Import failed


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 "", line 1, in
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



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...