Skip to content

Commit fd8e190

Browse files
committed
Avoid double draw in qt5cairo.
1 parent a3d1c46 commit fd8e190

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@ class FigureCanvasQTCairo(FigureCanvasQT, FigureCanvasCairo):
77
def __init__(self, figure):
88
super(FigureCanvasQTCairo, self).__init__(figure=figure)
99
self._renderer = RendererCairo(self.figure.dpi)
10+
self._renderer.set_width_height(-1, -1) # Invalid values.
11+
12+
def draw(self):
13+
if hasattr(self._renderer.gc, "ctx"):
14+
self.figure.draw(self._renderer)
15+
super(FigureCanvasQTCairo, self).draw()
1016

1117
def paintEvent(self, event):
1218
self._update_dpi()
1319
dpi_ratio = self._dpi_ratio
1420
width = dpi_ratio * self.width()
1521
height = dpi_ratio * self.height()
16-
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
17-
self._renderer.set_ctx_from_surface(surface)
18-
self._renderer.set_width_height(width, height)
19-
self.figure.draw(self._renderer)
20-
buf = surface.get_data()
22+
if (width, height) != self._renderer.get_canvas_width_height():
23+
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
24+
self._renderer.set_ctx_from_surface(surface)
25+
self._renderer.set_width_height(width, height)
26+
self.figure.draw(self._renderer)
27+
buf = self._renderer.gc.ctx.get_target().get_data()
2128
qimage = QtGui.QImage(buf, width, height,
2229
QtGui.QImage.Format_ARGB32_Premultiplied)
2330
# Adjust the buf reference count to work around a memory leak bug in
2431
# QImage under PySide on Python 3.
2532
if QT_API == 'PySide' and six.PY3:
33+
import ctypes
2634
ctypes.c_long.from_address(id(buf)).value = 1
2735
if hasattr(qimage, 'setDevicePixelRatio'):
2836
# Not available on Qt4 or some older Qt5.

0 commit comments

Comments
 (0)