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

Skip to content

Commit a4f22c6

Browse files
committed
pyqt4/pyside: use rcParams instead of environment variable
This provides easier control for the mpl user (keeping configuration in the rc system), uses the most conservative default (PyQt4), and does not alter the API version.
1 parent bd245ef commit a4f22c6

3 files changed

Lines changed: 30 additions & 33 deletions

File tree

lib/matplotlib/backends/qt4_compat.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,18 @@
33

44
import os
55
import warnings
6+
from matplotlib import rcParams
67

78
# Available APIs.
8-
QT_API_PYQT = 'pyqt'
9-
QT_API_PYSIDE = 'pyside'
9+
QT_API_PYQT = 'PyQt4'
10+
QT_API_PYSIDE = 'PySide'
1011

11-
def prepare_pyqt4():
12-
# For PySide compatibility, use the new-style string API that automatically
13-
# converts QStrings to Unicode Python strings. Also, automatically unpack
14-
# QVariants to their underlying objects.
15-
import sip
16-
sip.setapi('QString', 2)
17-
sip.setapi('QVariant', 2)
18-
19-
# Select Qt binding, using the QT_API environment variable if available.
20-
QT_API = os.environ.get('QT_API')
21-
if QT_API is None:
22-
try:
23-
import PySide
24-
if PySide.__version_info__ < (1,0,3):
25-
warnings.warn("PySide found with version < 1.0.3; trying PyQt4")
26-
raise ImportError
27-
QT_API = QT_API_PYSIDE
28-
except ImportError:
29-
try:
30-
prepare_pyqt4()
31-
import PyQt4
32-
QT_API = QT_API_PYQT
33-
except ImportError:
34-
raise ImportError('Cannot import PySide or PyQt4')
12+
# Select Qt binding, using the rcParams variable if available.
13+
QT_API = rcParams.setdefault('backend.qt4', QT_API_PYQT)
3514

36-
elif QT_API == QT_API_PYQT:
37-
# Note: This must be called *before* PyQt4 is imported.
38-
prepare_pyqt4()
15+
# We will define an appropriate wrapper for the differing versions
16+
# of file dialog.
17+
_getSaveFileName = None
3918

4019
# Now perform the imports.
4120
if QT_API == QT_API_PYQT:
@@ -51,8 +30,17 @@ def prepare_pyqt4():
5130
QtCore.Property = QtCore.pyqtProperty
5231
__version__ = QtCore.PYQT_VERSION_STR
5332

54-
# Use new getSaveFileNameAndFilter()
55-
_get_save = QtGui.QFileDialog.getSaveFileNameAndFilter
33+
import sip
34+
try :
35+
if sip.getapi("QString") > 1 :
36+
# Use new getSaveFileNameAndFilter()
37+
_get_save = QtGui.QFileDialog.getSaveFileNameAndFilter
38+
else :
39+
# Use old getSaveFileName()
40+
_getSaveFileName = QtGui.QFileDialog.getSaveFileName
41+
except (AttributeError, KeyError) :
42+
# call to getapi() can fail in older versions of sip
43+
_getSaveFileName = QtGui.QFileDialog.getSaveFileName
5644

5745
elif QT_API == QT_API_PYSIDE:
5846
from PySide import QtCore, QtGui, __version__, __version_info__
@@ -66,6 +54,8 @@ def prepare_pyqt4():
6654
raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' %
6755
(QT_API, QT_API_PYQT, QT_API_PYSIDE))
6856

69-
def _getSaveFileName(self, msg, start, filters, selectedFilter):
70-
return _get_save(self, msg, start, filters, selectedFilter)[0]
57+
if _getSaveFileName is None:
58+
59+
def _getSaveFileName(self, msg, start, filters, selectedFilter):
60+
return _get_save(self, msg, start, filters, selectedFilter)[0]
7161

lib/matplotlib/rcsetup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def validate_backend(s):
106106
if s.startswith('module://'): return s
107107
else: return _validate_standard_backends(s)
108108

109+
validate_qt4 = ValidateInStrings('backend.qt4', ['PyQt4', 'PySide'])
109110

110111
validate_toolbar = ValidateInStrings('toolbar',[
111112
'None','classic','toolbar2',
@@ -341,6 +342,7 @@ def __call__(self, s):
341342
defaultParams = {
342343
'backend' : ['Agg', validate_backend], # agg is certainly present
343344
'backend_fallback' : [True, validate_bool], # agg is certainly present
345+
'backend.qt4' : ['PyQt4', validate_qt4],
344346
'toolbar' : ['toolbar2', validate_toolbar],
345347
'datapath' : [None, validate_path_exists], # handled by _get_data_path_cached
346348
'interactive' : [False, validate_bool],

matplotlibrc.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
# 'module://my_backend'
3131
backend : %(backend)s
3232

33+
# If you are using the Qt4Agg backend, you can choose here
34+
# to use the PyQt4 bindings or the newer PySide bindings to
35+
# the underlying Qt4 toolkit.
36+
backend.qt4 : PyQt4
37+
3338
# if you are runing pyplot inside a GUI and your backend choice
3439
# conflicts, we will automatically try and find a compatible one for
3540
# you if backend_fallback is True

0 commit comments

Comments
 (0)