@@ -38,6 +38,15 @@ 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+
4150 def drawRectangle (self , rect ):
4251 if rect is not None :
4352 self ._drawRect = [pt / self ._dpi_ratio for pt in rect ]
@@ -56,11 +65,29 @@ def paintEvent(self, e):
5665 In Qt, all drawing should be done inside of here when a widget is
5766 shown onscreen.
5867 """
68+
5969 # if the canvas does not have a renderer, then give up and wait for
6070 # FigureCanvasAgg.draw(self) to be called
6171 if not hasattr (self , 'renderer' ):
6272 return
6373
74+ # As described in __init__ above, we need to be careful in cases with
75+ # mixed resolution displays if dpi_ratio is changing between painting
76+ # events.
77+ if (self ._dpi_ratio_prev is None or
78+ self ._dpi_ratio != self ._dpi_ratio_prev ):
79+ # We need to update the figure DPI
80+ self ._update_figure_dpi ()
81+ # The easiest way to resize the canvas is to emit a resizeEvent
82+ # since we implement all the logic for resizing the canvas for
83+ # that event.
84+ event = QtGui .QResizeEvent (self .size (), self .size ())
85+ # We use self.resizeEvent here instead of QApplication.postEvent
86+ # since the latter doesn't guarantee that the event will be emitted
87+ # straight away, and this causes visual delays in the changes.
88+ self .resizeEvent (event )
89+ self ._dpi_ratio_prev = self ._dpi_ratio
90+
6491 painter = QtGui .QPainter (self )
6592
6693 if self ._bbox_queue :
@@ -167,9 +194,12 @@ def __init__(self, figure):
167194 super (FigureCanvasQTAgg , self ).__init__ (figure = figure )
168195 # We don't want to scale up the figure DPI more than once.
169196 # Note, we don't handle a signal for changing DPI yet.
170- if not hasattr (self .figure , '_original_dpi' ):
171- self .figure ._original_dpi = self .figure .dpi
172- self .figure .dpi = self ._dpi_ratio * self .figure ._original_dpi
197+ self .figure ._original_dpi = self .figure .dpi
198+ self ._update_figure_dpi ()
199+
200+ def _update_figure_dpi (self ):
201+ dpi = self ._dpi_ratio * self .figure ._original_dpi
202+ self .figure ._set_dpi (dpi , forward = False )
173203
174204
175205@_BackendQT5 .export
0 commit comments