i was trying to use a QString in the QGIS Python Console.
from PyQt4.QtCore import QString
but it says:
ImportError: cannot
If Python2 is used with PyQt4, then classes like QString
and QVariant
will be available by default. However, the default for Python3 is to eliminate these classes and use the equivalent python types instead.
These defaults can be overridden by using the sip module, like this:
import sip
# switch on QString in Python3
sip.setapi('QString', 1)
# switch off QString in Python2
sip.setapi('QString', 2)
from PyQt4 import QtCore
If QString
is not available in QGIS, it's either because it's using Python3, or because it's using Python2 and switching APIs as suggested above.
Either way, probably the simplest fix for your issue would be to run the Python3 version of IDLE, so that you no longer have to bother with QString
and QVariant
.
Note that in PyQt5, there is no option to switch APIs, and so QString
will never be available there.
And also note that official support for Qt4 ends this year. So if you want to future-proof a new PyQt application, your first choice should be PyQt5 + Python3, unless you have good reasons for doing otherwise (e.g. unavoidable dependencies).