From 3365e72bec0ec441beb84948b67bd50fb1910713 Mon Sep 17 00:00:00 2001 From: James Evans Date: Wed, 12 Aug 2015 11:48:19 -0700 Subject: [PATCH] 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. --- lib/matplotlib/backends/qt_compat.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py index f237d449eb20..b949694034b3 100644 --- a/lib/matplotlib/backends/qt_compat.py +++ b/lib/matplotlib/backends/qt_compat.py @@ -26,8 +26,19 @@ if rcParams['backend'] == 'Qt5Agg': QT_RC_MAJOR_VERSION = 5 -else: +elif rcParams['backend'] == 'Qt4Agg': QT_RC_MAJOR_VERSION = 4 +else: + # A different backend was specified, but we still got here because a Qt + # related file was imported. This is allowed, so lets try and guess + # what we should be using. + try: + # if the newest API is available, lets assume that is being used. + import PyQt5 + QT_RC_MAJOR_VERSION = 5 + except ImportError: + # This is a fallback + QT_RC_MAJOR_VERSION = 4 QT_API = None @@ -47,8 +58,19 @@ # No ETS environment or incompatible so use rcParams. if rcParams['backend'] == 'Qt5Agg': QT_API = rcParams['backend.qt5'] - else: + elif rcParams['backend'] == 'Qt4Agg': QT_API = rcParams['backend.qt4'] + else: + # A different backend was specified, but we still got here because a Qt + # related file was imported. This is allowed, so lets try and guess + # what we should be using. + try: + # if the newest API is available, lets assume that is being used. + import PyQt5 + QT_API = rcParams['backend.qt5'] + except ImportError: + # This is a fallback + QT_API = rcParams['backend.qt4'] # We will define an appropriate wrapper for the differing versions # of file dialog.