PyQt: new API with Python 2

PyQt has two different APIs: old and new. By default, you get the old API with Python 2 and the new API with Python 3. Can I include the new PyQt API with Python 2? How?

+6
source share
3 answers

Perhaps you could try using sip.setapi . A simple example from the docs:

 import sip sip.setapi('QString', 2) 

And a list of supported APIs:

 QDate v1, v2 QDateTime v1, v2 QString v1, v2 QTextStream v1, v2 QTime v1, v2 QUrl v1, v2 QVariant v1, v2 
+8
source

From this reddit comment ,

 import sip API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"] API_VERSION = 2 for name in API_NAMES: sip.setapi(name, API_VERSION) from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtSvg import * from PyQt4.QtCore import pyqtSignal as Signal from PyQt4.QtCore import pyqtSlot as Slot 

(... although I would recommend from PyQt4 import QtCore , etc. instead of import * )

+12
source

Look at the โ€œIncompatible apisโ€ from the Riverbank website

PyQt provides limited support for several incompatible APIs and the ability for an application to choose between them at runtime. For example, an application can choose whether QString is implemented as a Python type, or is automatically converted to a Python v2 unicode object or a Python v3 string object.

This ability allows developers to decide how to manage the transition from the old legacy API to the newer incompatible API.

Each API that can be selected in this way has a name and a series of version numbers. An application calls sip.setapi () to set the version number of a specific API. This call must be made before any module that implements the API is imported. After installation, the version number cannot be changed. If it is not installed, the API will use its default version.

For example, the following code will disable the use of QString:

 import sip sip.setapi('QString', 2) from PyQt4 import QtCore # This will raise an attribute exception because QString is only wrapped # in version 1 of the API. s = QtCore.QString() 

The following APIs are currently implemented:

  • QDate v1, v2
  • QDateTime v1, v2
  • QString v1, v2
  • QTextStream v1, v2
  • QTime v1, v2
  • QUrl v1, v2
  • QVariant v1, v2
+4
source

Source: https://habr.com/ru/post/889776/


All Articles