qt.py
81 lines
| 2.9 KiB
| text/x-python
|
PythonLexer
Evan Patterson
|
r3304 | """ A Qt API selector that can be used to switch between PyQt and PySide. | ||
MinRK
|
r4191 | |||
This uses the ETS 4.0 selection pattern of: | ||||
PySide first, PyQt with API v2. second. | ||||
Do not use this if you need PyQt with the old QString/QVariant API. | ||||
Evan Patterson
|
r3304 | """ | ||
import os | ||||
MinRK
|
r9226 | from distutils.version import LooseVersion as V | ||
epatters
|
r3306 | # Available APIs. | ||
QT_API_PYQT = 'pyqt' | ||||
QT_API_PYSIDE = 'pyside' | ||||
epatters
|
r3927 | def prepare_pyqt4(): | ||
epatters
|
r3364 | # For PySide compatibility, use the new-style string API that automatically | ||
# converts QStrings to Unicode Python strings. Also, automatically unpack | ||||
# QVariants to their underlying objects. | ||||
Evan Patterson
|
r3304 | import sip | ||
sip.setapi('QString', 2) | ||||
epatters
|
r3364 | sip.setapi('QVariant', 2) | ||
Evan Patterson
|
r3304 | |||
epatters
|
r3927 | # Select Qt binding, using the QT_API environment variable if available. | ||
QT_API = os.environ.get('QT_API') | ||||
if QT_API is None: | ||||
Julian Taylor
|
r9006 | pyside_found = False | ||
epatters
|
r3927 | try: | ||
import PySide | ||||
MinRK
|
r9226 | if V(PySide.__version__) < V('1.0.3'): | ||
MinRK
|
r4677 | # old PySide, fallback on PyQt | ||
raise ImportError | ||||
Julian Taylor
|
r9006 | # we can't import an incomplete pyside and pyqt4 | ||
# this will cause a crash in sip (#1431) | ||||
# check for complete presence before importing | ||||
import imp | ||||
imp.find_module("QtCore", PySide.__path__) | ||||
imp.find_module("QtGui", PySide.__path__) | ||||
imp.find_module("QtSvg", PySide.__path__) | ||||
pyside_found = True | ||||
Julian Taylor
|
r6722 | from PySide import QtCore, QtGui, QtSvg | ||
epatters
|
r3927 | QT_API = QT_API_PYSIDE | ||
except ImportError: | ||||
try: | ||||
prepare_pyqt4() | ||||
import PyQt4 | ||||
Julian Taylor
|
r6722 | from PyQt4 import QtCore, QtGui, QtSvg | ||
Julian Taylor
|
r9006 | if pyside_found: | ||
print "WARNING: PySide installation incomplete and PyQt4 " \ | ||||
"present.\nThis will likely crash, please install " \ | ||||
"PySide completely, remove PySide or PyQt4 or set " \ | ||||
"the QT_API environment variable to pyqt or pyside" | ||||
MinRK
|
r9226 | if V(QtCore.PYQT_VERSION_STR) < V('4.7'): | ||
MinRK
|
r4677 | # PyQt 4.6 has issues with null strings returning as None | ||
raise ImportError | ||||
epatters
|
r3927 | QT_API = QT_API_PYQT | ||
except ImportError: | ||||
MinRK
|
r4677 | raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7') | ||
Bernardo B. Marques
|
r4872 | |||
epatters
|
r3927 | elif QT_API == QT_API_PYQT: | ||
# Note: This must be called *before* PyQt4 is imported. | ||||
prepare_pyqt4() | ||||
# Now peform the imports. | ||||
if QT_API == QT_API_PYQT: | ||||
Evan Patterson
|
r3304 | from PyQt4 import QtCore, QtGui, QtSvg | ||
MinRK
|
r9226 | if V(QtCore.PYQT_VERSION_STR) < V('4.7'): | ||
MinRK
|
r4677 | raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR) | ||
Evan Patterson
|
r3304 | |||
# Alias PyQt-specific functions for PySide compatibility. | ||||
QtCore.Signal = QtCore.pyqtSignal | ||||
QtCore.Slot = QtCore.pyqtSlot | ||||
epatters
|
r3306 | elif QT_API == QT_API_PYSIDE: | ||
MinRK
|
r4677 | import PySide | ||
MinRK
|
r9226 | if V(PySide.__version__) < V('1.0.3'): | ||
MinRK
|
r4677 | raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__) | ||
Evan Patterson
|
r3304 | from PySide import QtCore, QtGui, QtSvg | ||
epatters
|
r3306 | |||
else: | ||||
Bernardo B. Marques
|
r4872 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % | ||
epatters
|
r3334 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) | ||