6

PyQt has two different API's: the old and the new. By default you get the old API with Python 2 and the new API with Python 3. Is it possible to enable the new PyQt API with Python 2? How?

Johan Råde
  • 20,480
  • 21
  • 73
  • 110

3 Answers3

13

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

dbr
  • 165,801
  • 69
  • 278
  • 343
  • just curious: why would you recommend that? it leads to code like `QtWebKit.QWebSettings.globalSettings().setAttribute(QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)`, which is horrible enough without `QtWebKit.` – flying sheep Sep 27 '12 at 12:40
  • 3
    @flyingsheep This is late, but you should do `from QtWebkit import QWebSettings`. Explicitly importing what you need helps static-checkers have a prayer of keeping track of what is in your namespace and keeps you from surprisingly clobbering something (ex `from numpy import *` clobbers `sum` and you then `sum(generator)` no longer works) – tacaswell Jun 07 '14 at 19:54
9

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

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

And a list of the supported APIs:

QDate v1, v2
QDateTime v1, v2
QString v1, v2
QTextStream v1, v2
QTime v1, v2
QUrl v1, v2
QVariant v1, v2
senderle
  • 145,869
  • 36
  • 209
  • 233
  • That solves part of the problem (QString, QVariant etc.) Can I get also get the new signal and connections API with PyQt and Python 2? – Johan Råde Jun 04 '11 at 20:00
  • 1
    The new signal and connection API is enabled by default in Python 2. You can switch from connect(foo, SIGNAL('clicked'), bar) to foo.clicked.connect(bar) or even interleave them — they co-exist just fine. – PAG Jun 04 '11 at 23:55
  • 1
    This is now a dead link; I was able to get what I needed from dbr's post referencing the reddit link. – guyincognito Mar 07 '13 at 00:25
4

Look at "Incompatible apis" from Riverbank site

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

This ability allows developers to decide how to manage the transition from an older deprecated, API to a newer incompatible API.

Each API that can be selected in this way has a name and a range of version numbers. An application calls sip.setapi() to set the version number of a particular API. This call must be made before any module that implements the API is imported. Once set the version number cannot be changed. If not set then an 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
tacaswell
  • 84,579
  • 22
  • 210
  • 199
ivanalejandro0
  • 1,689
  • 13
  • 13