1 # -*- coding: utf-8 -*-
3 # Copyright © 2009- The Spyder Development Team
4 # Copyright © 2014-2015 Colin Duquesnoy
6 # Licensed under the terms of the MIT License
7 # (see LICENSE.txt for details)
10 **QtPy** is a shim over the various Python Qt bindings. It is used to write
11 Qt binding indenpendent libraries or applications.
13 If one of the APIs has already been imported, then it will be used.
15 Otherwise, the shim will automatically select the first available API (PyQt5,
16 PySide2, PyQt4 and finally PySide); in that case, you can force the use of one
17 specific bindings (e.g. if your application is using one specific bindings and
18 you need to use library that use QtPy) by setting up the ``QT_API`` environment
24 For PyQt5, you don't have to set anything as it will be used automatically::
26 >>> from qtpy import QtGui, QtWidgets, QtCore
27 >>> print(QtWidgets.QWidget)
33 Set the QT_API environment variable to 'pyside2' before importing other
37 >>> os.environ['QT_API'] = 'pyside2'
38 >>> from qtpy import QtGui, QtWidgets, QtCore
39 >>> print(QtWidgets.QWidget)
44 Set the ``QT_API`` environment variable to 'pyqt' before importing any python
48 >>> os.environ['QT_API'] = 'pyqt'
49 >>> from qtpy import QtGui, QtWidgets, QtCore
50 >>> print(QtWidgets.QWidget)
55 Set the QT_API environment variable to 'pyside' before importing other
59 >>> os.environ['QT_API'] = 'pyside'
60 >>> from qtpy import QtGui, QtWidgets, QtCore
61 >>> print(QtWidgets.QWidget)
70 from ._version
import __version__
73 class PythonQtError(RuntimeError):
74 """Error raise if no bindings could be selected."""
78 class PythonQtWarning(Warning):
79 """Warning if some features are not implemented in a binding."""
83 # Qt API environment variable name
86 # Names of the expected PyQt5 api
89 # Names of the expected PyQt4 api
91 'pyqt', # name used in IPython.qt
92 'pyqt4' # pyqode.qt original name
95 # Names of the expected PySide api
96 PYSIDE_API
= ['pyside']
98 # Names of the expected PySide2 api
99 PYSIDE2_API
= ['pyside2']
101 # Setting a default value for QT_API
102 os
.environ
.setdefault(QT_API
, 'pyqt5')
104 API
= os
.environ
[QT_API
].lower()
106 assert API
in (PYQT5_API
+ PYQT4_API
+ PYSIDE_API
+ PYSIDE2_API
)
108 is_old_pyqt
= is_pyqt46
= False
110 PYQT4
= PYSIDE
= PYSIDE2
= False
113 if 'PyQt5' in sys
.modules
:
115 elif 'PySide2' in sys
.modules
:
117 elif 'PyQt4' in sys
.modules
:
119 elif 'PySide' in sys
.modules
:
125 from PyQt5
.QtCore
import PYQT_VERSION_STR
as PYQT_VERSION
# analysis:ignore
126 from PyQt5
.QtCore
import QT_VERSION_STR
as QT_VERSION
# analysis:ignore
127 PYSIDE_VERSION
= None
129 API
= os
.environ
['QT_API'] = 'pyside2'
131 if API
in PYSIDE2_API
:
133 from PySide2
import __version__
as PYSIDE_VERSION
# analysis:ignore
134 from PySide2
.QtCore
import __version__
as QT_VERSION
# analysis:ignore
140 API
= os
.environ
['QT_API'] = 'pyqt'
146 sip
.setapi('QString', 2)
147 sip
.setapi('QVariant', 2)
148 sip
.setapi('QDate', 2)
149 sip
.setapi('QDateTime', 2)
150 sip
.setapi('QTextStream', 2)
151 sip
.setapi('QTime', 2)
152 sip
.setapi('QUrl', 2)
153 except (AttributeError, ValueError):
156 from PyQt4
.Qt
import PYQT_VERSION_STR
as PYQT_VERSION
# analysis:ignore
157 from PyQt4
.Qt
import QT_VERSION_STR
as QT_VERSION
# analysis:ignore
158 PYSIDE_VERSION
= None
162 API
= os
.environ
['QT_API'] = 'pyside'
164 is_old_pyqt
= PYQT_VERSION
.startswith(('4.4', '4.5', '4.6', '4.7'))
165 is_pyqt46
= PYQT_VERSION
.startswith('4.6')
167 if API
in PYSIDE_API
:
169 from PySide
import __version__
as PYSIDE_VERSION
# analysis:ignore
170 from PySide
.QtCore
import __version__
as QT_VERSION
# analysis:ignore
172 PYQT5
= PYSIDE2
= False
175 raise PythonQtError('No Qt bindings could be found')
177 API_NAME
= {'pyqt5': 'PyQt5', 'pyqt': 'PyQt4', 'pyqt4': 'PyQt4',
178 'pyside': 'PySide', 'pyside2':'PySide2'}[API
]
183 API_NAME
+= (" (API v{0})".format(sip
.getapi('QString')))
184 except AttributeError: