PyQt:python2的新API

2024-10-01 11:39:14 发布

您现在位置:Python中文网/ 问答频道 /正文

PyQt有两个不同的API:旧的和新的。 默认情况下,使用python2获得旧API,使用python3获得新API。 可以用python2启用新的pyqtapi吗?怎样?在


Tags: api情况pyqtpython3python2pyqtapi
3条回答

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

(…虽然我建议使用from PyQt4 import QtCore等代替import *

也许您可以尝试使用^{}。文档中的一个简单示例:

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

以及支持的API列表:

^{pr2}$

看看Riverbank的“不兼容api”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

相关问题 更多 >