Show More
@@ -1,32 +1,49 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 |
|
3 | |||
4 | import os |
|
4 | import os | |
5 |
|
5 | |||
6 | # Available APIs. |
|
6 | # Available APIs. | |
7 | QT_API_PYQT = 'pyqt' |
|
7 | QT_API_PYQT = 'pyqt' | |
8 | QT_API_PYSIDE = 'pyside' |
|
8 | QT_API_PYSIDE = 'pyside' | |
9 |
|
9 | |||
10 | # Use PyQt by default until PySide is stable. |
|
10 | def prepare_pyqt4(): | |
11 | QT_API = os.environ.get('QT_API', QT_API_PYQT) |
|
|||
12 |
|
||||
13 | if QT_API == QT_API_PYQT: |
|
|||
14 | # For PySide compatibility, use the new-style string API that automatically |
|
11 | # For PySide compatibility, use the new-style string API that automatically | |
15 | # converts QStrings to Unicode Python strings. Also, automatically unpack |
|
12 | # converts QStrings to Unicode Python strings. Also, automatically unpack | |
16 | # QVariants to their underlying objects. |
|
13 | # QVariants to their underlying objects. | |
17 | import sip |
|
14 | import sip | |
18 | sip.setapi('QString', 2) |
|
15 | sip.setapi('QString', 2) | |
19 | sip.setapi('QVariant', 2) |
|
16 | sip.setapi('QVariant', 2) | |
20 |
|
17 | |||
|
18 | # Select Qt binding, using the QT_API environment variable if available. | |||
|
19 | QT_API = os.environ.get('QT_API') | |||
|
20 | if QT_API is None: | |||
|
21 | try: | |||
|
22 | import PySide | |||
|
23 | QT_API = QT_API_PYSIDE | |||
|
24 | except ImportError: | |||
|
25 | try: | |||
|
26 | prepare_pyqt4() | |||
|
27 | import PyQt4 | |||
|
28 | QT_API = QT_API_PYQT | |||
|
29 | except ImportError: | |||
|
30 | raise ImportError('Cannot import PySide or PyQt4') | |||
|
31 | ||||
|
32 | elif QT_API == QT_API_PYQT: | |||
|
33 | # Note: This must be called *before* PyQt4 is imported. | |||
|
34 | prepare_pyqt4() | |||
|
35 | ||||
|
36 | # Now peform the imports. | |||
|
37 | if QT_API == QT_API_PYQT: | |||
21 | from PyQt4 import QtCore, QtGui, QtSvg |
|
38 | from PyQt4 import QtCore, QtGui, QtSvg | |
22 |
|
39 | |||
23 | # Alias PyQt-specific functions for PySide compatibility. |
|
40 | # Alias PyQt-specific functions for PySide compatibility. | |
24 | QtCore.Signal = QtCore.pyqtSignal |
|
41 | QtCore.Signal = QtCore.pyqtSignal | |
25 | QtCore.Slot = QtCore.pyqtSlot |
|
42 | QtCore.Slot = QtCore.pyqtSlot | |
26 |
|
43 | |||
27 | elif QT_API == QT_API_PYSIDE: |
|
44 | elif QT_API == QT_API_PYSIDE: | |
28 | from PySide import QtCore, QtGui, QtSvg |
|
45 | from PySide import QtCore, QtGui, QtSvg | |
29 |
|
46 | |||
30 | else: |
|
47 | else: | |
31 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % |
|
48 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % | |
32 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
|
49 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
General Comments 0
You need to be logged in to leave comments.
Login now