##// END OF EJS Templates
Merge pull request #1704 from juliantaylor/ensure-qt-imports...
Min RK -
r6730:c4d651a9 merge
parent child Browse files
Show More
@@ -1,66 +1,67 b''
1 """ A Qt API selector that can be used to switch between PyQt and PySide.
1 """ A Qt API selector that can be used to switch between PyQt and PySide.
2
2
3 This uses the ETS 4.0 selection pattern of:
3 This uses the ETS 4.0 selection pattern of:
4 PySide first, PyQt with API v2. second.
4 PySide first, PyQt with API v2. second.
5
5
6 Do not use this if you need PyQt with the old QString/QVariant API.
6 Do not use this if you need PyQt with the old QString/QVariant API.
7 """
7 """
8
8
9 import os
9 import os
10
10
11 # Available APIs.
11 # Available APIs.
12 QT_API_PYQT = 'pyqt'
12 QT_API_PYQT = 'pyqt'
13 QT_API_PYSIDE = 'pyside'
13 QT_API_PYSIDE = 'pyside'
14
14
15 def prepare_pyqt4():
15 def prepare_pyqt4():
16 # For PySide compatibility, use the new-style string API that automatically
16 # For PySide compatibility, use the new-style string API that automatically
17 # converts QStrings to Unicode Python strings. Also, automatically unpack
17 # converts QStrings to Unicode Python strings. Also, automatically unpack
18 # QVariants to their underlying objects.
18 # QVariants to their underlying objects.
19 import sip
19 import sip
20 sip.setapi('QString', 2)
20 sip.setapi('QString', 2)
21 sip.setapi('QVariant', 2)
21 sip.setapi('QVariant', 2)
22
22
23 # Select Qt binding, using the QT_API environment variable if available.
23 # Select Qt binding, using the QT_API environment variable if available.
24 QT_API = os.environ.get('QT_API')
24 QT_API = os.environ.get('QT_API')
25 if QT_API is None:
25 if QT_API is None:
26 try:
26 try:
27 import PySide
27 import PySide
28 if PySide.__version__ < '1.0.3':
28 if PySide.__version__ < '1.0.3':
29 # old PySide, fallback on PyQt
29 # old PySide, fallback on PyQt
30 raise ImportError
30 raise ImportError
31 from PySide import QtCore, QtGui, QtSvg
31 QT_API = QT_API_PYSIDE
32 QT_API = QT_API_PYSIDE
32 except ImportError:
33 except ImportError:
33 try:
34 try:
34 prepare_pyqt4()
35 prepare_pyqt4()
35 import PyQt4
36 import PyQt4
36 from PyQt4 import QtCore
37 from PyQt4 import QtCore, QtGui, QtSvg
37 if QtCore.PYQT_VERSION_STR < '4.7':
38 if QtCore.PYQT_VERSION_STR < '4.7':
38 # PyQt 4.6 has issues with null strings returning as None
39 # PyQt 4.6 has issues with null strings returning as None
39 raise ImportError
40 raise ImportError
40 QT_API = QT_API_PYQT
41 QT_API = QT_API_PYQT
41 except ImportError:
42 except ImportError:
42 raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7')
43 raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7')
43
44
44 elif QT_API == QT_API_PYQT:
45 elif QT_API == QT_API_PYQT:
45 # Note: This must be called *before* PyQt4 is imported.
46 # Note: This must be called *before* PyQt4 is imported.
46 prepare_pyqt4()
47 prepare_pyqt4()
47
48
48 # Now peform the imports.
49 # Now peform the imports.
49 if QT_API == QT_API_PYQT:
50 if QT_API == QT_API_PYQT:
50 from PyQt4 import QtCore, QtGui, QtSvg
51 from PyQt4 import QtCore, QtGui, QtSvg
51 if QtCore.PYQT_VERSION_STR < '4.7':
52 if QtCore.PYQT_VERSION_STR < '4.7':
52 raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR)
53 raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR)
53
54
54 # Alias PyQt-specific functions for PySide compatibility.
55 # Alias PyQt-specific functions for PySide compatibility.
55 QtCore.Signal = QtCore.pyqtSignal
56 QtCore.Signal = QtCore.pyqtSignal
56 QtCore.Slot = QtCore.pyqtSlot
57 QtCore.Slot = QtCore.pyqtSlot
57
58
58 elif QT_API == QT_API_PYSIDE:
59 elif QT_API == QT_API_PYSIDE:
59 import PySide
60 import PySide
60 if PySide.__version__ < '1.0.3':
61 if PySide.__version__ < '1.0.3':
61 raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__)
62 raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__)
62 from PySide import QtCore, QtGui, QtSvg
63 from PySide import QtCore, QtGui, QtSvg
63
64
64 else:
65 else:
65 raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' %
66 raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' %
66 (QT_API, QT_API_PYQT, QT_API_PYSIDE))
67 (QT_API, QT_API_PYQT, QT_API_PYSIDE))
General Comments 0
You need to be logged in to leave comments. Login now