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

Skip to content

FIX: shim Qt4 and Qt5 together better #9048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@

class FigureCanvasQT(FigureCanvasQT5):

def __init__(self, figure):
if DEBUG:
print('FigureCanvasQt qt4: ', figure)
_create_qApp()

def _fake_super_fcq(self, figure):
# Note different super-calling style to backend_qt5
QtWidgets.QWidget.__init__(self)
FigureCanvasBase.__init__(self, figure)
self.figure = figure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd all this other stuff, like setMouseTracking go away?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that stuff is handled in the FigureCanvasQt5 init and this function injected in.

self.setMouseTracking(True)
self._idle = True
w, h = self.get_width_height()
self.resize(w, h)

# Key auto-repeat enabled by default
self._keyautorepeat = True
# Do not call this here (even though it looks like we should!)
# because it will be called in the Agg backend and we want to 'break'
# the MI diamond. With PyQt5 (which in cooperative) this is handled by
# super
# FigureCanvasBase.__init__(self, figure)

def wheelEvent(self, event):
x = event.x()
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/backends/backend_qt4agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from .backend_qt5agg import FigureCanvasQTAggBase


class FigureCanvasQTAggBase(FigureCanvasQTAggBase):
def _fake_super_fcqab(self, figure):
FigureCanvasAgg.__init__(self, figure)


class FigureCanvasQTAgg(FigureCanvasQTAggBase, FigureCanvasQT):
"""
The canvas the figure renders into. Calls the draw and print fig
Expand All @@ -23,6 +28,9 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase, FigureCanvasQT):
A high-level Figure instance

"""
def __init__(self, figure):
FigureCanvasQT.__init__(self, figure)
FigureCanvasQTAggBase.__init__(self, figure)


@_BackendQT4.export
Expand Down
13 changes: 8 additions & 5 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def _timer_stop(self):
self._timer.stop()


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

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

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

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

def _fake_super_fcq(self, figure):
super(FigureCanvasQT, self).__init__(figure=figure)

@property
def _dpi_ratio(self):
# Not available on Qt4 or some older Qt5.
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FigureCanvasQTAggBase(FigureCanvasAgg):
"""

def __init__(self, figure):
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
self._fake_super_fcqab(figure)
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
self._agg_draw_pending = False
self._bbox_queue = []
Expand All @@ -47,6 +47,9 @@ def __init__(self, figure):
# needed.
self._dpi_ratio_prev = None

def _fake_super_fcqab(self, figure):
super(FigureCanvasQTAggBase, self).__init__(figure=figure)

def drawRectangle(self, rect):
if rect is not None:
self._drawRect = [pt / self._dpi_ratio for pt in rect]
Expand Down