@@ -227,12 +227,8 @@ def __init__(self, figure):
227227 self ._update_figure_dpi ()
228228 # In cases with mixed resolution displays, we need to be careful if the
229229 # dpi_ratio changes - in this case we need to resize the canvas
230- # accordingly. We could watch for screenChanged events from Qt, but
231- # the issue is that we can't guarantee this will be emitted *before*
232- # the first paintEvent for the canvas, so instead we keep track of the
233- # dpi_ratio value here and in paintEvent we resize the canvas if
234- # needed.
235- self ._dpi_ratio_prev = None
230+ # accordingly.
231+ self ._dpi_ratio_prev = self ._dpi_ratio
236232
237233 self ._draw_pending = False
238234 self ._is_drawing = False
@@ -253,12 +249,9 @@ def _update_figure_dpi(self):
253249 def _dpi_ratio (self ):
254250 return _devicePixelRatioF (self )
255251
256- def _update_dpi (self ):
257- # As described in __init__ above, we need to be careful in cases with
258- # mixed resolution displays if dpi_ratio is changing between painting
259- # events.
260- # Return whether we triggered a resizeEvent (and thus a paintEvent)
261- # from within this function.
252+ def _update_pixel_ratio (self ):
253+ # We need to be careful in cases with mixed resolution displays if
254+ # dpi_ratio changes.
262255 if self ._dpi_ratio != self ._dpi_ratio_prev :
263256 # We need to update the figure DPI.
264257 self ._update_figure_dpi ()
@@ -270,8 +263,20 @@ def _update_dpi(self):
270263 self .resizeEvent (event )
271264 # resizeEvent triggers a paintEvent itself, so we exit this one
272265 # (after making sure that the event is immediately handled).
273- return True
274- return False
266+
267+ def _update_screen (self , screen ):
268+ # Handler for changes to a window's attached screen.
269+ self ._update_pixel_ratio ()
270+ if screen is not None :
271+ screen .physicalDotsPerInchChanged .connect (self ._update_pixel_ratio )
272+ screen .logicalDotsPerInchChanged .connect (self ._update_pixel_ratio )
273+
274+ def showEvent (self , event ):
275+ # Set up correct pixel ratio, and connect to any signal changes for it,
276+ # once the window is shown (and thus has these attributes).
277+ window = self .window ().windowHandle ()
278+ window .screenChanged .connect (self ._update_screen )
279+ self ._update_screen (window .screen ())
275280
276281 def get_width_height (self ):
277282 w , h = FigureCanvasBase .get_width_height (self )
@@ -364,10 +369,6 @@ def keyReleaseEvent(self, event):
364369 FigureCanvasBase .key_release_event (self , key , guiEvent = event )
365370
366371 def resizeEvent (self , event ):
367- # _dpi_ratio_prev will be set the first time the canvas is painted, and
368- # the rendered buffer is useless before anyways.
369- if self ._dpi_ratio_prev is None :
370- return
371372 w = event .size ().width () * self ._dpi_ratio
372373 h = event .size ().height () * self ._dpi_ratio
373374 dpival = self .figure .dpi
0 commit comments