@@ -7,22 +7,30 @@ class FigureCanvasQTCairo(FigureCanvasQT, FigureCanvasCairo):
7
7
def __init__ (self , figure ):
8
8
super (FigureCanvasQTCairo , self ).__init__ (figure = figure )
9
9
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 ()
10
16
11
17
def paintEvent (self , event ):
12
18
self ._update_dpi ()
13
19
dpi_ratio = self ._dpi_ratio
14
20
width = dpi_ratio * self .width ()
15
21
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 ()
21
28
qimage = QtGui .QImage (buf , width , height ,
22
29
QtGui .QImage .Format_ARGB32_Premultiplied )
23
30
# Adjust the buf reference count to work around a memory leak bug in
24
31
# QImage under PySide on Python 3.
25
32
if QT_API == 'PySide' and six .PY3 :
33
+ import ctypes
26
34
ctypes .c_long .from_address (id (buf )).value = 1
27
35
if hasattr (qimage , 'setDevicePixelRatio' ):
28
36
# Not available on Qt4 or some older Qt5.
0 commit comments