Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a50214

Browse files
committed
qt_compat.py: support the ETS environment variable QT_API
1 parent a4f22c6 commit 9a50214

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

lib/matplotlib/backends/qt4_compat.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,40 @@
66
from matplotlib import rcParams
77

88
# Available APIs.
9-
QT_API_PYQT = 'PyQt4'
10-
QT_API_PYSIDE = 'PySide'
9+
QT_API_PYQT = 'PyQt4' # API is not set here; Python 2.x default is V 1
10+
QT_API_PYQTv2 = 'PyQt4v2' # forced to Version 2 API
11+
QT_API_PYSIDE = 'PySide' # only supports Version 2 API
1112

12-
# Select Qt binding, using the rcParams variable if available.
13-
QT_API = rcParams.setdefault('backend.qt4', QT_API_PYQT)
13+
ETS = dict(pyqt=QT_API_PYQTv2, pyside=QT_API_PYSIDE)
14+
15+
# If the ETS QT_API environment variable is set, use it. Note that
16+
# ETS requires the version 2 of PyQt4, which is not the platform
17+
# default for Python 2.x.
18+
19+
QT_API_ENV = os.environ.get('QT_API')
20+
if QT_API_ENV is not None:
21+
try:
22+
QT_API = ETS[QT_API_ENV]
23+
except KeyError:
24+
raise RuntimeError(
25+
'Unrecognized environment variable %r, valid values are: %r or %r' %
26+
(QT_API_ENV, 'pyqt', 'pyside'))
27+
else:
28+
# No ETS environment, so use rcParams.
29+
QT_API = rcParams['backend.qt4']
1430

1531
# We will define an appropriate wrapper for the differing versions
1632
# of file dialog.
1733
_getSaveFileName = None
1834

1935
# Now perform the imports.
20-
if QT_API == QT_API_PYQT:
21-
from PyQt4 import QtCore, QtGui, QtSvg
36+
if QT_API in (QT_API_PYQT, QT_API_PYQTv2):
37+
import sip
38+
if QT_API == QT_API_PYQTv2:
39+
sip.setapi('QString', 2)
40+
sip.setapi('QVariant', 2)
41+
42+
from PyQt4 import QtCore, QtGui
2243

2344
# Alias PyQt-specific functions for PySide compatibility.
2445
QtCore.Signal = QtCore.pyqtSignal
@@ -30,7 +51,6 @@
3051
QtCore.Property = QtCore.pyqtProperty
3152
__version__ = QtCore.PYQT_VERSION_STR
3253

33-
import sip
3454
try :
3555
if sip.getapi("QString") > 1 :
3656
# Use new getSaveFileNameAndFilter()
@@ -42,17 +62,14 @@
4262
# call to getapi() can fail in older versions of sip
4363
_getSaveFileName = QtGui.QFileDialog.getSaveFileName
4464

45-
elif QT_API == QT_API_PYSIDE:
65+
else: # can only be pyside
4666
from PySide import QtCore, QtGui, __version__, __version_info__
4767
if __version_info__ < (1,0,3):
4868
raise ImportError(
4969
"Matplotlib backend_qt4 and backend_qt4agg require PySide >=1.0.3")
5070

5171
_get_save = QtGui.QFileDialog.getSaveFileName
5272

53-
else:
54-
raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' %
55-
(QT_API, QT_API_PYQT, QT_API_PYSIDE))
5673

5774
if _getSaveFileName is None:
5875

matplotlibrc.template

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ backend : %(backend)s
3333
# If you are using the Qt4Agg backend, you can choose here
3434
# to use the PyQt4 bindings or the newer PySide bindings to
3535
# the underlying Qt4 toolkit.
36-
backend.qt4 : PyQt4
36+
#backend.qt4 : PyQt4 # PyQt4 | PySide
37+
38+
# Note that this can be overridden by the environment variable
39+
# QT_API used by Enthought Tool Suite (ETS); valid values are
40+
# "pyqt" and "pyside". The "pyqt" setting has the side effect of
41+
# forcing the use of Version 2 API for QString and QVariant.
3742

3843
# if you are runing pyplot inside a GUI and your backend choice
39-
# conflicts, we will automatically try and find a compatible one for
44+
# conflicts, we will automatically try to find a compatible one for
4045
# you if backend_fallback is True
4146
#backend_fallback: True
47+
4248
#interactive : False
4349
#toolbar : toolbar2 # None | classic | toolbar2
4450
#timezone : UTC # a pytz timezone string, eg US/Central or Europe/Paris

0 commit comments

Comments
 (0)