Skip to content

Commit b181d50

Browse files
committed
FIX: shim Qt4 and Qt5 together better
in two places `super` needs to be used in the Qt5 case and frustrated in the Qt4 case. These are handled by the two `_fake_super_*` methods. The must be just private as we need to override in sub-classes. Closes matplotlib#9040
1 parent 3769792 commit b181d50

4 files changed

Lines changed: 26 additions & 20 deletions

File tree

‎lib/matplotlib/backends/backend_qt4.py‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,14 @@
2828

2929
class FigureCanvasQT(FigureCanvasQT5):
3030

31-
def __init__(self, figure):
32-
if DEBUG:
33-
print('FigureCanvasQt qt4: ', figure)
34-
_create_qApp()
35-
31+
def _fake_super_fcq(self, figure):
3632
# Note different super-calling style to backend_qt5
3733
QtWidgets.QWidget.__init__(self)
38-
FigureCanvasBase.__init__(self, figure)
39-
self.figure = figure
40-
self.setMouseTracking(True)
41-
self._idle = True
42-
w, h = self.get_width_height()
43-
self.resize(w, h)
44-
45-
# Key auto-repeat enabled by default
46-
self._keyautorepeat = True
34+
# Do not call this here (even though it looks like we should!)
35+
# because it will be called in the Agg backend and we want to 'break'
36+
# the MI diamond. With PyQt5 (which in cooperative) this is handled by
37+
# super
38+
# FigureCanvasBase.__init__(self, figure)
4739

4840
def wheelEvent(self, event):
4941
x = event.x()

‎lib/matplotlib/backends/backend_qt4agg.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from .backend_qt5agg import FigureCanvasQTAggBase
1313

1414

15+
class FigureCanvasQTAggBase(FigureCanvasQTAggBase):
16+
def _fake_super_fcqab(self, figure):
17+
FigureCanvasAgg.__init__(self, figure)
18+
19+
1520
class FigureCanvasQTAgg(FigureCanvasQTAggBase, FigureCanvasQT):
1621
"""
1722
The canvas the figure renders into. Calls the draw and print fig
@@ -23,6 +28,9 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase, FigureCanvasQT):
2328
A high-level Figure instance
2429
2530
"""
31+
def __init__(self, figure):
32+
FigureCanvasQT.__init__(self, figure)
33+
FigureCanvasQTAggBase.__init__(self, figure)
2634

2735

2836
@_BackendQT4.export

‎lib/matplotlib/backends/backend_qt5.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def _timer_stop(self):
172172
self._timer.stop()
173173

174174

175+
# FigureCanvasBase is not cooperative so it most go last here
176+
# or the super-chain will be truncated early.
175177
class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
176178

177179
# map Qt button codes to MouseEvent's ones:
@@ -190,12 +192,10 @@ def __init__(self, figure):
190192
_create_qApp()
191193
figure._original_dpi = figure.dpi
192194

193-
# NB: Using super for this call to avoid a TypeError:
194-
# __init__() takes exactly 2 arguments (1 given) on QWidget
195-
# PyQt5
196-
# The need for this change is documented here
195+
# PyQt5 does cooperative MI so we can just use `super`, PyQt4
196+
# and PySide do not so we have to fake it.
197197
# http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#cooperative-multi-inheritance
198-
super(FigureCanvasQT, self).__init__(figure=figure)
198+
self._fake_super_fcq(figure)
199199
self.figure = figure
200200
self._update_figure_dpi()
201201

@@ -206,6 +206,9 @@ def __init__(self, figure):
206206
# Key auto-repeat enabled by default
207207
self._keyautorepeat = True
208208

209+
def _fake_super_fcq(self, figure):
210+
super(FigureCanvasQT, self).__init__(figure=figure)
211+
209212
@property
210213
def _dpi_ratio(self):
211214
# Not available on Qt4 or some older Qt5.

‎lib/matplotlib/backends/backend_qt5agg.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FigureCanvasQTAggBase(FigureCanvasAgg):
3232
"""
3333

3434
def __init__(self, figure):
35-
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
35+
self._fake_super_fcqab(figure)
3636
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
3737
self._agg_draw_pending = False
3838
self._bbox_queue = []
@@ -47,6 +47,9 @@ def __init__(self, figure):
4747
# needed.
4848
self._dpi_ratio_prev = None
4949

50+
def _fake_super_fcqab(self, figure):
51+
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
52+
5053
def drawRectangle(self, rect):
5154
if rect is not None:
5255
self._drawRect = [pt / self._dpi_ratio for pt in rect]

0 commit comments

Comments
 (0)