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 The shim will automatically select the first available API (PyQt5, PyQt4 and
16 You can force the use of one specific bindings (e.g. if your application is
17 using one specific bindings and you need to use library that use QtPy) by
18 setting up the ``QT_API`` environment variable.
23 For PyQt5, you don't have to set anything as it will be used automatically::
25 >>> from qtpy import QtGui, QtWidgets, QtCore
26 >>> print(QtWidgets.QWidget)
32 Set the ``QT_API`` environment variable to 'pyqt' before importing any python
36 >>> os.environ['QT_API'] = 'pyqt'
37 >>> from qtpy import QtGui, QtWidgets, QtCore
38 >>> print(QtWidgets.QWidget)
43 Set the QT_API environment variable to 'pyside' before importing other
47 >>> os.environ['QT_API'] = 'pyside'
48 >>> from qtpy import QtGui, QtWidgets, QtCore
49 >>> print(QtWidgets.QWidget)
56 from qtpy
._version
import __version__
58 #: Qt API environment variable name
60 #: names of the expected PyQt5 api
62 #: names of the expected PyQt4 api
64 'pyqt', # name used in IPython.qt
65 'pyqt4' # pyqode.qt original name
67 #: names of the expected PySide api
68 PYSIDE_API
= ['pyside']
70 os
.environ
.setdefault(QT_API
, 'pyqt5')
71 API
= os
.environ
[QT_API
]
72 assert API
in (PYQT5_API
+ PYQT4_API
+ PYSIDE_API
)
74 is_old_pyqt
= is_pyqt46
= False
76 PYQT4
= PYSIDE
= False
79 class PythonQtError(Exception):
80 """Error raise if no bindings could be selected"""
86 from PyQt5
.Qt
import PYQT_VERSION_STR
as PYQT_VERSION
# analysis:ignore
87 from PyQt5
.Qt
import QT_VERSION_STR
as QT_VERSION
# analysis:ignore
90 API
= os
.environ
['QT_API'] = 'pyqt'
96 sip
.setapi('QString', 2)
97 sip
.setapi('QVariant', 2)
98 sip
.setapi('QDate', 2)
99 sip
.setapi('QDateTime', 2)
100 sip
.setapi('QTextStream', 2)
101 sip
.setapi('QTime', 2)
102 sip
.setapi('QUrl', 2)
103 except AttributeError:
106 from PyQt4
.Qt
import PYQT_VERSION_STR
as PYQT_VERSION
# analysis:ignore
107 from PyQt4
.Qt
import QT_VERSION_STR
as QT_VERSION
# analysis:ignore
108 PYSIDE_VERSION
= None
112 API
= os
.environ
['QT_API'] = 'pyside'
114 is_old_pyqt
= PYQT_VERSION
.startswith(('4.4', '4.5', '4.6', '4.7'))
115 is_pyqt46
= PYQT_VERSION
.startswith('4.6')
117 if API
in PYSIDE_API
:
119 from PySide
import __version__
as PYSIDE_VERSION
# analysis:ignore
120 from PySide
.QtCore
import __version__
as QT_VERSION
# analysis:ignore
125 raise PythonQtError('No Qt bindings could be found')
127 API_NAME
= {'pyqt5': 'PyQt5', 'pyqt': 'PyQt4', 'pyqt4': 'PyQt4',
128 'pyside': 'PySide'}[API
]
132 API_NAME
+= (" (API v{0})".format(sip
.getapi('QString')))
133 except AttributeError: