PyQt: new API with Python 2

前端 未结 3 500
终归单人心
终归单人心 2020-12-10 17:52

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 wit

相关标签:
3条回答
  • 2020-12-10 18:09

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

    0 讨论(0)
  • 2020-12-10 18:19

    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
    
    0 讨论(0)
  • 2020-12-10 18:21

    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
    0 讨论(0)
提交回复
热议问题