5
5
- if any of PyQt6, PySide6, PyQt5, or PySide2 have already been
6
6
imported (checked in that order), use it;
7
7
- otherwise, if the QT_API environment variable (used by Enthought) is set, use
8
- it to determine which binding to use (but do not change the backend based on
9
- it; i.e. if the Qt5Agg backend is requested but QT_API is set to "pyqt4",
10
- then actually use Qt5 with PyQt5 or PySide2 (whichever can be imported);
8
+ it to determine which binding to use;
11
9
- otherwise, use whatever the rcParams indicate.
12
10
"""
13
11
31
29
QT_API_PYSIDE6 = "PySide6"
32
30
QT_API_PYQT5 = "PyQt5"
33
31
QT_API_PYSIDE2 = "PySide2"
34
- QT_API_PYQTv2 = "PyQt4v2"
35
- QT_API_PYSIDE = "PySide"
36
- QT_API_PYQT = "PyQt4" # Use the old sip v1 API (Py3 defaults to v2).
37
32
QT_API_ENV = os .environ .get ("QT_API" )
38
33
if QT_API_ENV is not None :
39
34
QT_API_ENV = QT_API_ENV .lower ()
40
- # Mapping of QT_API_ENV to requested binding. ETS does not support PyQt4v1.
41
- # (https://github.com/enthought/pyface/blob/master/pyface/qt/__init__.py)
42
- _ETS = {
35
+ _ETS = { # Mapping of QT_API_ENV to requested binding.
43
36
"pyqt6" : QT_API_PYQT6 , "pyside6" : QT_API_PYSIDE6 ,
44
37
"pyqt5" : QT_API_PYQT5 , "pyside2" : QT_API_PYSIDE2 ,
45
- None : None
46
38
}
47
39
# First, check if anything is already imported.
48
40
if sys .modules .get ("PyQt6.QtCore" ):
73
65
# fully manually embedding Matplotlib in a Qt app without using pyplot).
74
66
elif QT_API_ENV is None :
75
67
QT_API = None
68
+ elif QT_API_ENV in _ETS :
69
+ QT_API = _ETS [QT_API_ENV ]
76
70
else :
77
- try :
78
- QT_API = _ETS [QT_API_ENV ]
79
- except KeyError :
80
- raise RuntimeError (
81
- "The environment variable QT_API has the unrecognized value "
82
- f"{ QT_API_ENV !r} ; "
83
- f"valid values are { set (k for k in _ETS if k is not None )} "
84
- ) from None
71
+ raise RuntimeError (
72
+ "The environment variable QT_API has the unrecognized value {!r}; "
73
+ "valid values are {}" .format (QT_API_ENV , ", " .join (_ETS )))
85
74
86
75
87
76
def _setup_pyqt5plus ():
@@ -139,7 +128,9 @@ def _isdeleted(obj):
139
128
continue
140
129
break
141
130
else :
142
- raise ImportError ("Failed to import any qt binding" )
131
+ raise ImportError (
132
+ "Failed to import any of the following Qt binding modules: {}"
133
+ .format (", " .join (_ETS .values ())))
143
134
else : # We should not get there.
144
135
raise AssertionError (f"Unexpected QT_API: { QT_API } " )
145
136
@@ -186,7 +177,7 @@ def _devicePixelRatioF(obj):
186
177
except AttributeError :
187
178
pass
188
179
try :
189
- # Not available on Qt4 or some older Qt5.
180
+ # Not available on older Qt5.
190
181
# self.devicePixelRatio() returns 0 in rare cases
191
182
return obj .devicePixelRatio () or 1
192
183
except AttributeError :
@@ -200,7 +191,7 @@ def _setDevicePixelRatio(obj, val):
200
191
This can be replaced by the direct call when we require Qt>=5.6.
201
192
"""
202
193
if hasattr (obj , 'setDevicePixelRatio' ):
203
- # Not available on Qt4 or some older Qt5.
194
+ # Not available on older Qt5.
204
195
obj .setDevicePixelRatio (val )
205
196
206
197
0 commit comments