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

Skip to content

Commit 46d3828

Browse files
committed
Don't call draw() twice when Qt canvas first appears.
Right now the resizeEvent triggered from within the paintEvent will itself trigger a second paintEvent. Adding a print to draw() shows that this patch ensures that draw() only gets called once the first time the canvas appears.
1 parent df6acf9 commit 46d3828

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ def __init__(self, figure):
249249
# Key auto-repeat enabled by default
250250
self._keyautorepeat = True
251251

252+
# In cases with mixed resolution displays, we need to be careful if the
253+
# dpi_ratio changes - in this case we need to resize the canvas
254+
# accordingly. We could watch for screenChanged events from Qt, but
255+
# the issue is that we can't guarantee this will be emitted *before*
256+
# the first paintEvent for the canvas, so instead we keep track of the
257+
# dpi_ratio value here and in paintEvent we resize the canvas if
258+
# needed.
259+
self._dpi_ratio_prev = None
260+
252261
@property
253262
def _dpi_ratio(self):
254263
# Not available on Qt4 or some older Qt5.
@@ -342,6 +351,10 @@ def keyAutoRepeat(self, val):
342351
self._keyautorepeat = bool(val)
343352

344353
def resizeEvent(self, event):
354+
# _dpi_ratio_prev will be set the first time the canvas is painted, and
355+
# the rendered buffer is useless before anyways.
356+
if self._dpi_ratio_prev is None:
357+
return
345358
w = event.size().width() * self._dpi_ratio
346359
h = event.size().height() * self._dpi_ratio
347360
dpival = self.figure.dpi

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from .backend_agg import FigureCanvasAgg
1616
from .backend_qt5 import (
17-
QtCore, QtGui, _BackendQT5, FigureCanvasQT, FigureManagerQT,
17+
QtCore, QtGui, QtWidgets, _BackendQT5, FigureCanvasQT, FigureManagerQT,
1818
NavigationToolbar2QT, backend_version)
1919
from .qt_compat import QT_API
2020

@@ -38,15 +38,6 @@ def __init__(self, figure):
3838
self._bbox_queue = []
3939
self._drawRect = None
4040

41-
# In cases with mixed resolution displays, we need to be careful if the
42-
# dpi_ratio changes - in this case we need to resize the canvas
43-
# accordingly. We could watch for screenChanged events from Qt, but
44-
# the issue is that we can't guarantee this will be emitted *before*
45-
# the first paintEvent for the canvas, so instead we keep track of the
46-
# dpi_ratio value here and in paintEvent we resize the canvas if
47-
# needed.
48-
self._dpi_ratio_prev = None
49-
5041
def drawRectangle(self, rect):
5142
if rect is not None:
5243
self._drawRect = [pt / self._dpi_ratio for pt in rect]
@@ -66,18 +57,13 @@ def paintEvent(self, e):
6657
shown onscreen.
6758
"""
6859

69-
# if the canvas does not have a renderer, then give up and wait for
70-
# FigureCanvasAgg.draw(self) to be called
71-
if not hasattr(self, 'renderer'):
72-
return
73-
7460
# As described in __init__ above, we need to be careful in cases with
7561
# mixed resolution displays if dpi_ratio is changing between painting
7662
# events.
77-
if (self._dpi_ratio_prev is None or
78-
self._dpi_ratio != self._dpi_ratio_prev):
63+
if self._dpi_ratio != self._dpi_ratio_prev:
7964
# We need to update the figure DPI
8065
self._update_figure_dpi()
66+
self._dpi_ratio_prev = self._dpi_ratio
8167
# The easiest way to resize the canvas is to emit a resizeEvent
8268
# since we implement all the logic for resizing the canvas for
8369
# that event.
@@ -86,7 +72,14 @@ def paintEvent(self, e):
8672
# since the latter doesn't guarantee that the event will be emitted
8773
# straight away, and this causes visual delays in the changes.
8874
self.resizeEvent(event)
89-
self._dpi_ratio_prev = self._dpi_ratio
75+
QtWidgets.QApplication.instance().processEvents()
76+
# resizeEvent triggers a paintEvent itself, so we exit this one.
77+
return
78+
79+
# if the canvas does not have a renderer, then give up and wait for
80+
# FigureCanvasAgg.draw(self) to be called
81+
if not hasattr(self, 'renderer'):
82+
return
9083

9184
painter = QtGui.QPainter(self)
9285

0 commit comments

Comments
 (0)