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

Skip to content

Commit 87c9e4d

Browse files
committed
Merge pull request #5134 from JGoutin/PyQt5-import-fix
MNT: qt imports fix
2 parents 0c11514 + 4daacdd commit 87c9e4d

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 "PyQt4" in sys.modules or "PySide" in sys.modules:
37+
# PyQt4 or PySide is actually used.
38+
QT_RC_MAJOR_VERSION = 4
39+
else:
40+
# This is a fallback: PyQt5
41+
QT_RC_MAJOR_VERSION = 5
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 "PyQt4" in sys.modules or "PySide" in sys.modules:
68+
# PyQt4 or PySide is actually used.
69+
QT_API = rcParams['backend.qt4']
70+
else:
71+
# This is a fallback: PyQt5
72+
QT_API = rcParams['backend.qt5']
5273

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

0 commit comments

Comments
 (0)