我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用sip.setapi()。
def load_pyqt4(): """Sets up PyQt4 nicely to be PySide-compatible as much as possible.""" # Kill off QString and QVariant to make it act properly like PySide import sip sip.setapi('QString', 2) sip.setapi('QVariant', 2) from PyQt4 import QtCore QtCore._QT_ENGINE = 'PyQt4' # Also rename the pyqt things for compatibility. QtCore.Signal = QtCore.pyqtSignal QtCore.Slot = QtCore.pyqtSlot QtCore.Property = QtCore.pyqtProperty global IS_PYQT4 IS_PYQT4 = True
def autoselect(): """Selects PySide or PyQt4.""" try: # Don't want to import as we need to sip.setapi first imp.find_module('PyQt4') except ImportError: try: # Don't want to import two copies (as PySide and as PyQt4) imp.find_module('PySide') except ImportError: raise ImportError('Neither PySide nor PyQt4 is installed.') else: load_pyside() else: load_pyqt4() zip_imp.cleanup()
def _pyqt4(): # Attempt to set sip API v2 (must be done prior to importing PyQt4) import sip try: sip.setapi("QString", 2) sip.setapi("QVariant", 2) sip.setapi("QDate", 2) sip.setapi("QDateTime", 2) sip.setapi("QTextStream", 2) sip.setapi("QTime", 2) sip.setapi("QUrl", 2) except AttributeError: raise ImportError # PyQt4 < v4.6 except ValueError: # API version already set to v1 raise ImportError import PyQt4.Qt # Remap PyQt4.QtWidgets = PyQt4.QtGui PyQt4.QtCore.Signal = PyQt4.QtCore.pyqtSignal PyQt4.QtCore.Slot = PyQt4.QtCore.pyqtSlot PyQt4.QtCore.Property = PyQt4.QtCore.pyqtProperty PyQt4.QtCore.QItemSelection = PyQt4.QtGui.QItemSelection PyQt4.QtCore.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel # Add PyQt4.__wrapper_version__ = __version__ PyQt4.__binding__ = "PyQt4" PyQt4.__binding_version__ = PyQt4.QtCore.PYQT_VERSION_STR PyQt4.__qt_version__ = PyQt4.QtCore.QT_VERSION_STR PyQt4.load_ui = pyqt4_load_ui return PyQt4
def import_pyqt4(version=2): """ Import PyQt4 Parameters ---------- version : 1, 2, or None Which QString/QVariant API to use. Set to None to use the system default ImportErrors rasied within this function are non-recoverable """ # The new-style string API (version=2) automatically # converts QStrings to Unicode Python strings. Also, automatically unpacks # QVariants to their underlying objects. import sip if version is not None: sip.setapi('QString', version) sip.setapi('QVariant', version) from PyQt4 import QtGui, QtCore, QtSvg if not check_version(QtCore.PYQT_VERSION_STR, '4.7'): raise ImportError("IPython requires PyQt4 >= 4.7, found %s" % QtCore.PYQT_VERSION_STR) # Alias PyQt-specific functions for PySide compatibility. QtCore.Signal = QtCore.pyqtSignal QtCore.Slot = QtCore.pyqtSlot # query for the API version (in case version == None) version = sip.getapi('QString') api = QT_API_PYQTv1 if version == 1 else QT_API_PYQT return QtCore, QtGui, QtSvg, api
def import_pyqt4(version=2): """ Import PyQt4 Parameters ---------- version : 1, 2, or None Which QString/QVariant API to use. Set to None to use the system default ImportErrors raised within this function are non-recoverable """ # The new-style string API (version=2) automatically # converts QStrings to Unicode Python strings. Also, automatically unpacks # QVariants to their underlying objects. import sip if version is not None: sip.setapi('QString', version) sip.setapi('QVariant', version) from PyQt4 import QtGui, QtCore, QtSvg if not check_version(QtCore.PYQT_VERSION_STR, '4.7'): raise ImportError("IPython requires PyQt4 >= 4.7, found %s" % QtCore.PYQT_VERSION_STR) # Alias PyQt-specific functions for PySide compatibility. QtCore.Signal = QtCore.pyqtSignal QtCore.Slot = QtCore.pyqtSlot # query for the API version (in case version == None) version = sip.getapi('QString') api = QT_API_PYQTv1 if version == 1 else QT_API_PYQT return QtCore, QtGui, QtSvg, api
def test_sip_api_already_set(): """Raise ImportError with sip was set to 1 with no hint, default""" __import__("PyQt4.QtCore") # Bypass linter warning import sip sip.setapi("QString", 1) assert_raises(ImportError, __import__, "Qt") # A sip API hint of any kind bypasses ImportError # on account of it being merely a hint.
def test_sip_api_1_1(): """sip=1, hint=1 == OK""" import sip sip.setapi("QString", 1) os.environ["QT_SIP_API_HINT"] = "1" __import__("Qt") # Bypass linter warning
def test_sip_api_2_1(): """sip=2, hint=1 == WARNING""" import sip sip.setapi("QString", 2) os.environ["QT_SIP_API_HINT"] = "1" with captured_output() as out: __import__("Qt") # Bypass linter warning stdout, stderr = out assert stderr.getvalue().startswith("Warning:")
def test_sip_api_1_2(): """sip=1, hint=2 == WARNING""" import sip sip.setapi("QString", 1) os.environ["QT_SIP_API_HINT"] = "2" with captured_output() as out: __import__("Qt") # Bypass linter warning stdout, stderr = out assert stderr.getvalue().startswith("Warning:")
def test_sip_api_2_2(): """sip=2, hint=2 == OK""" import sip sip.setapi("QString", 2) os.environ["QT_SIP_API_HINT"] = "2" __import__("Qt") # Bypass linter warning
def _pyqt4(): import sip try: sip.setapi("QString", 2) sip.setapi("QVariant", 2) sip.setapi("QDate", 2) sip.setapi("QDateTime", 2) sip.setapi("QTextStream", 2) sip.setapi("QTime", 2) sip.setapi("QUrl", 2) except AttributeError as e: raise ImportError(str(e)) # PyQt4 < v4.6 except ValueError as e: # API version already set to v1 raise ImportError(str(e)) from PyQt4 import ( QtGui, QtCore, QtNetwork, QtXml, QtHelp, uic ) QtWidgets = QtGui Qt.__binding__ = "PyQt4" Qt.__qt_version__ = QtCore.QT_VERSION_STR Qt.__binding_version__ = QtCore.PYQT_VERSION_STR QtCompat.load_ui = lambda fname: uic.loadUi(fname) QtCompat.setSectionResizeMode = QtGui.QHeaderView.setResizeMode # PySide2 differs from Qt4 in that Qt4 has one extra argument # which is always `None`. The lambda arguments represents the PySide2 # interface, whereas the arguments passed to `.translate` represent # those expected of a Qt4 binding. QtCompat.translate = ( lambda context, sourceText, disambiguation, n: QtCore.QCoreApplication.translate(context, sourceText, disambiguation, QtCore.QCoreApplication.CodecForTr, n)) return QtCore, QtGui, QtWidgets, QtNetwork, QtXml, QtHelp
def _pyqt4(): # Attempt to set sip API v2 (must be done prior to importing PyQt4) import sip try: sip.setapi("QString", 2) sip.setapi("QVariant", 2) sip.setapi("QDate", 2) sip.setapi("QDateTime", 2) sip.setapi("QTextStream", 2) sip.setapi("QTime", 2) sip.setapi("QUrl", 2) except AttributeError: raise ImportError # PyQt4 < v4.6 except ValueError: # API version already set to v1 raise ImportError import PyQt4.Qt from PyQt4 import QtCore, QtGui, uic _remap(PyQt4, "QtWidgets", QtGui) _remap(QtCore, "Signal", QtCore.pyqtSignal) _remap(QtCore, "Slot", QtCore.pyqtSlot) _remap(QtCore, "Property", QtCore.pyqtProperty) _remap(QtCore, "QItemSelection", QtGui.QItemSelection) _remap(QtCore, "QStringListModel", QtGui.QStringListModel) _remap(QtCore, "QItemSelectionModel", QtGui.QItemSelectionModel) _remap(QtCore, "QSortFilterProxyModel", QtGui.QSortFilterProxyModel) _remap(QtCore, "QAbstractProxyModel", QtGui.QAbstractProxyModel) try: from PyQt4 import QtWebKit _remap(PyQt4, "QtWebKitWidgets", QtWebKit) except ImportError: # QtWebkit is optional in Qt , therefore might not be available pass _add(PyQt4, "QtCompat", self) _add(PyQt4, "__binding__", PyQt4.__name__) _add(PyQt4, "load_ui", lambda fname: uic.loadUi(fname)) _add(PyQt4, "translate", lambda context, sourceText, disambiguation, n: ( QtCore.QCoreApplication(context, sourceText, disambiguation, None, n))) _add(PyQt4, "setSectionResizeMode", QtGui.QHeaderView.setResizeMode) _maintain_backwards_compatibility(PyQt4) return PyQt4
def pyqt4(): # Attempt to set sip API v2 (must be done prior to importing PyQt4) import sip try: sip.setapi("QString", 2) sip.setapi("QVariant", 2) sip.setapi("QDate", 2) sip.setapi("QDateTime", 2) sip.setapi("QTextStream", 2) sip.setapi("QTime", 2) sip.setapi("QUrl", 2) except AttributeError: raise ImportError # PyQt4 < v4.6 except ValueError: # API version already set to v1 raise ImportError import PyQt4.Qt from PyQt4 import QtCore, QtGui, uic remap(PyQt4, "QtWidgets", QtGui) remap(QtCore, "Signal", QtCore.pyqtSignal) remap(QtCore, "Slot", QtCore.pyqtSlot) remap(QtCore, "Property", QtCore.pyqtProperty) remap(QtCore, "QItemSelection", QtGui.QItemSelection) remap(QtCore, "QStringListModel", QtGui.QStringListModel) remap(QtCore, "QItemSelectionModel", QtGui.QItemSelectionModel) remap(QtCore, "QSortFilterProxyModel", QtGui.QSortFilterProxyModel) remap(QtCore, "QAbstractProxyModel", QtGui.QAbstractProxyModel) try: from PyQt4 import QtWebKit remap(PyQt4, "QtWebKitWidgets", QtWebKit) except ImportError: # QtWebkit is optional in Qt , therefore might not be available pass add(PyQt4, "__wrapper_version__", __version__) add(PyQt4, "__binding__", "PyQt4") add(PyQt4, "__binding_version__", QtCore.PYQT_VERSION_STR) add(PyQt4, "__qt_version__", QtCore.QT_VERSION_STR) add(PyQt4, "__added__", __added__) add(PyQt4, "__remapped__", __remapped__) add(PyQt4, "__modified__", __modified__) add(PyQt4, "load_ui", lambda fname: uic.loadUi(fname)) return PyQt4
def _pyqt4(): # Attempt to set sip API v2 (must be done prior to importing PyQt4) import sip try: sip.setapi("QString", 2) sip.setapi("QVariant", 2) sip.setapi("QDate", 2) sip.setapi("QDateTime", 2) sip.setapi("QTextStream", 2) sip.setapi("QTime", 2) sip.setapi("QUrl", 2) except AttributeError: raise ImportError # PyQt4 < v4.6 except ValueError: # API version already set to v1 raise ImportError import PyQt4.Qt from PyQt4 import QtCore, QtGui, uic _remap(PyQt4, "QtWidgets", QtGui) _remap(QtCore, "Signal", QtCore.pyqtSignal) _remap(QtCore, "Slot", QtCore.pyqtSlot) _remap(QtCore, "Property", QtCore.pyqtProperty) _remap(QtCore, "QItemSelection", QtGui.QItemSelection) _remap(QtCore, "QStringListModel", QtGui.QStringListModel) _remap(QtCore, "QItemSelectionModel", QtGui.QItemSelectionModel) _remap(QtCore, "QSortFilterProxyModel", QtGui.QSortFilterProxyModel) _remap(QtCore, "QAbstractProxyModel", QtGui.QAbstractProxyModel) try: from PyQt4 import QtWebKit _remap(PyQt4, "QtWebKitWidgets", QtWebKit) except ImportError: "QtWebkit is optional in Qt , therefore might not be available" _add(QtCompat, "__binding__", PyQt4.__name__) _add(QtCompat, "__binding_version__", PyQt4.QtCore.PYQT_VERSION_STR) _add(QtCompat, "__qt_version__", PyQt4.QtCore.QT_VERSION_STR) _add(QtCompat, "load_ui", lambda fname: uic.loadUi(fname)) _add(QtCompat, "setSectionResizeMode", QtGui.QHeaderView.setResizeMode) # PySide2 differs from Qt4 in that Qt4 has one extra argument # which is always `None`. The lambda arguments represents the PySide2 # interface, whereas the arguments passed to `.translate` represent # those expected of a Qt4 binding. _add(QtCompat, "translate", lambda context, sourceText, disambiguation, n: QtCore.QCoreApplication.translate(context, sourceText, disambiguation, QtCore.QCoreApplication.CodecForTr, n)) _maintain_backwards_compatibility(PyQt4) return PyQt4