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

Skip to content

Commit f8cb5c9

Browse files
committed
Fix a bug with the Qt5 backend with mixed resolution displays
When mixed-resolution displays are present, the _dpi_ratio attribute on the canvas may change between paintEvents, which means that we need to change the size of the canvas. However, the underlying canvas is only resized if a resizeEvent is emitted, so we check whether _dpi_ratio has changed between paintEvents, and if so we emit a fake resizeEvent for the widget.
1 parent a5d9254 commit f8cb5c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ 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 needed.
47+
self._dpi_ratio_prev = None
48+
4149
def drawRectangle(self, rect):
4250
if rect is not None:
4351
self._drawRect = [pt / self._dpi_ratio for pt in rect]
@@ -56,6 +64,23 @@ def paintEvent(self, e):
5664
In Qt, all drawing should be done inside of here when a widget is
5765
shown onscreen.
5866
"""
67+
68+
# As described in __init__ above, we need to be careful in cases with
69+
# mixed resolution displays if dpi_ratio is changing between painting
70+
# events.
71+
if self._dpi_ratio_prev is None:
72+
self._dpi_ratio_prev = self._dpi_ratio
73+
elif self._dpi_ratio != self._dpi_ratio_prev:
74+
# The easiest way to resize the canvas is to emit a resizeEvent
75+
# since we implement all the logic for resizing the canvas for
76+
# that event.
77+
event = QtGui.QResizeEvent(self.size(), self.size())
78+
# We use self.resizeEvent here instead of QApplication.postEvent
79+
# since the latter doesn't guarantee that the event will be emitted
80+
# straight away, and this causes visual delays in the changes.
81+
self.resizeEvent(event)
82+
self._dpi_ratio_prev = self._dpi_ratio
83+
5984
# if the canvas does not have a renderer, then give up and wait for
6085
# FigureCanvasAgg.draw(self) to be called
6186
if not hasattr(self, 'renderer'):

0 commit comments

Comments
 (0)