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

Skip to content

Commit e143f62

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 1f8d143 commit e143f62

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def stop_event_loop(self, event=None):
426426

427427

428428
class MainWindow(QtWidgets.QMainWindow):
429+
429430
closing = QtCore.Signal()
430431

431432
def closeEvent(self, event):

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .backend_qt5 import (
1818
QtCore, QtGui, FigureCanvasQT, FigureManagerQT, NavigationToolbar2QT,
1919
backend_version, draw_if_interactive, show)
20+
from . import backend_qt5
2021
from .qt_compat import QT_API
2122

2223

@@ -56,6 +57,14 @@ def __init__(self, figure):
5657
self._bbox_queue = []
5758
self._drawRect = None
5859

60+
# In cases with mixed resolution displays, we need to be careful if the
61+
# dpi_ratio changes - in this case we need to resize the canvas
62+
# accordingly. We could watch for screenChanged events from Qt, but
63+
# the issue is that we can't guarantee this will be emitted *before*
64+
# the first paintEvent for the canvas, so instead we keep track of the
65+
# dpi_ratio value here and in paintEvent we resize the canvas if needed.
66+
self._dpi_ratio_prev = None
67+
5968
def drawRectangle(self, rect):
6069
if rect is not None:
6170
self._drawRect = [pt / self._dpi_ratio for pt in rect]
@@ -74,6 +83,23 @@ def paintEvent(self, e):
7483
In Qt, all drawing should be done inside of here when a widget is
7584
shown onscreen.
7685
"""
86+
87+
# As described in __init__ above, we need to be careful in cases with
88+
# mixed resolution displays if dpi_ratio is changing between painting
89+
# events.
90+
if self._dpi_ratio_prev is None:
91+
self._dpi_ratio_prev = self._dpi_ratio
92+
elif self._dpi_ratio != self._dpi_ratio_prev:
93+
# The easiest way to resize the canvas is to emit a resizeEvent
94+
# since we implement all the logic for resizing the canvas for
95+
# that event.
96+
event = QtGui.QResizeEvent(self.size(), self.size())
97+
# We use self.resizeEvent here instead of QApplication.postEvent
98+
# since the latter doesn't guarantee that the event will be emitted
99+
# straight away, and this causes visual delays in the changes.
100+
self.resizeEvent(event)
101+
self._dpi_ratio_prev = self._dpi_ratio
102+
77103
# if the canvas does not have a renderer, then give up and wait for
78104
# FigureCanvasAgg.draw(self) to be called
79105
if not hasattr(self, 'renderer'):

0 commit comments

Comments
 (0)