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" ):
68
60
# fully manually embedding Matplotlib in a Qt app without using pyplot).
69
61
elif QT_API_ENV is None :
70
62
QT_API = None
63
+ elif QT_API_ENV in _ETS :
64
+ QT_API = _ETS [QT_API_ENV ]
71
65
else :
72
- try :
73
- QT_API = _ETS [QT_API_ENV ]
74
- except KeyError :
75
- raise RuntimeError (
76
- "The environment variable QT_API has the unrecognized value "
77
- f"{ QT_API_ENV !r} ; "
78
- f"valid values are { set (k for k in _ETS if k is not None )} "
79
- ) from None
66
+ raise RuntimeError (
67
+ "The environment variable QT_API has the unrecognized value {!r}; "
68
+ "valid values are {}" .format (QT_API_ENV , ", " .join (_ETS )))
80
69
81
70
82
71
def _setup_pyqt5plus ():
@@ -134,7 +123,9 @@ def _isdeleted(obj):
134
123
continue
135
124
break
136
125
else :
137
- raise ImportError ("Failed to import any qt binding" )
126
+ raise ImportError (
127
+ "Failed to import any of the following Qt binding modules: {}"
128
+ .format (", " .join (_ETS .values ())))
138
129
else : # We should not get there.
139
130
raise AssertionError (f"Unexpected QT_API: { QT_API } " )
140
131
@@ -181,7 +172,7 @@ def _devicePixelRatioF(obj):
181
172
except AttributeError :
182
173
pass
183
174
try :
184
- # Not available on Qt4 or some older Qt5.
175
+ # Not available on older Qt5.
185
176
# self.devicePixelRatio() returns 0 in rare cases
186
177
return obj .devicePixelRatio () or 1
187
178
except AttributeError :
@@ -195,7 +186,7 @@ def _setDevicePixelRatio(obj, val):
195
186
This can be replaced by the direct call when we require Qt>=5.6.
196
187
"""
197
188
if hasattr (obj , 'setDevicePixelRatio' ):
198
- # Not available on Qt4 or some older Qt5.
189
+ # Not available on older Qt5.
199
190
obj .setDevicePixelRatio (val )
200
191
201
192
0 commit comments