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

Skip to content

Commit 2a23a63

Browse files
committed
Fix PyQt4/PyQt5 backend autodetection
This fixes an error where PyQt4 would always be selected in the case where a Qt related file was imported, but not set as the current backend Change from jrevans commit : Check what PyQt version is actually used rather than try to force the use of PyQt5.
1 parent 7900ee4 commit 2a23a63

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/matplotlib/backends/qt_compat.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from matplotlib.externals import six
77

88
import os
9+
import sys
910
from matplotlib import rcParams, verbose
1011

1112
# Available APIs.
@@ -26,8 +27,18 @@
2627

2728
if rcParams['backend'] == 'Qt5Agg':
2829
QT_RC_MAJOR_VERSION = 5
29-
else:
30+
elif rcParams['backend'] == 'Qt4Agg':
3031
QT_RC_MAJOR_VERSION = 4
32+
else:
33+
# A different backend was specified, but we still got here because a Qt
34+
# related file was imported. This is allowed, so lets try and guess
35+
# what we should be using.
36+
if "PyQt5" in sys.modules:
37+
# PyQt5 is actually used.
38+
QT_RC_MAJOR_VERSION = 5
39+
else:
40+
# This is a fallback
41+
QT_RC_MAJOR_VERSION = 4
3142

3243
QT_API = None
3344

@@ -47,8 +58,18 @@
4758
# No ETS environment or incompatible so use rcParams.
4859
if rcParams['backend'] == 'Qt5Agg':
4960
QT_API = rcParams['backend.qt5']
50-
else:
61+
elif rcParams['backend'] == 'Qt4Agg':
5162
QT_API = rcParams['backend.qt4']
63+
else:
64+
# A different backend was specified, but we still got here because a Qt
65+
# related file was imported. This is allowed, so lets try and guess
66+
# what we should be using.
67+
if "PyQt5" in sys.modules:
68+
# PyQt5 is actually used.
69+
QT_API = rcParams['backend.qt5']
70+
else:
71+
# This is a fallback
72+
QT_API = rcParams['backend.qt4']
5273

5374
# We will define an appropriate wrapper for the differing versions
5475
# of file dialog.

0 commit comments

Comments
 (0)