QGIS PyQt4 missing QString class

不羁的心 提交于 2019-12-01 21:26:51

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).

I've not used QGIS, but this is probably because the PyQt has been switched to the new API version 2 (see http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html). In the new API, any Qt function returning or taking a QString takes a Python native string instead. The new API is much more convenient and is the default for PyQt5.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!