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

Skip to content

Commit 9eb07e9

Browse files
committed
Fix backwards compatibility with Qt4.
1 parent 4f809f4 commit 9eb07e9

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,17 @@ def __init__(self, figure):
243243
w, h = self.get_width_height()
244244
self.resize(w, h)
245245

246+
@property
247+
def _dpi_ratio(self):
248+
# Not available on Qt4 or some older Qt5.
249+
try:
250+
return self.devicePixelRatio()
251+
except AttributeError:
252+
return 1
253+
246254
def get_width_height(self):
247-
dpi_ratio = self.devicePixelRatio()
248255
w, h = FigureCanvasBase.get_width_height(self)
249-
return int(w / dpi_ratio), int(h / dpi_ratio)
256+
return int(w / self._dpi_ratio), int(h / self._dpi_ratio)
250257

251258
def enterEvent(self, event):
252259
FigureCanvasBase.enter_notify_event(self, guiEvent=event)
@@ -256,9 +263,9 @@ def leaveEvent(self, event):
256263
FigureCanvasBase.leave_notify_event(self, guiEvent=event)
257264

258265
def mouseEventCoords(self, pos):
259-
x = pos.x() * self.devicePixelRatio()
266+
x = pos.x() * self._dpi_ratio
260267
# flip y so y=0 is bottom of canvas
261-
y = self.figure.bbox.height - pos.y() * self.devicePixelRatio()
268+
y = self.figure.bbox.height - pos.y() * self._dpi_ratio
262269
return x, y
263270

264271
def mousePressEvent(self, event):
@@ -325,9 +332,8 @@ def keyReleaseEvent(self, event):
325332
print('key release', key)
326333

327334
def resizeEvent(self, event):
328-
dpi_ratio = self.devicePixelRatio()
329-
w = event.size().width() * dpi_ratio
330-
h = event.size().height() * dpi_ratio
335+
w = event.size().width() * self._dpi_ratio
336+
h = event.size().height() * self._dpi_ratio
331337
if DEBUG:
332338
print('resize (%d x %d)' % (w, h))
333339
print("FigureCanvasQt.resizeEvent(%d, %d)" % (w, h))

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def paintEvent(self, e):
104104
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
105105
self.renderer.height,
106106
QtGui.QImage.Format_ARGB32)
107-
qImage.setDevicePixelRatio(self._dpi_ratio)
107+
if hasattr(qImage, 'setDevicePixelRatio'):
108+
# Not available on Qt4 or some older Qt5.
109+
qImage.setDevicePixelRatio(self._dpi_ratio)
108110
# get the rectangle for the image
109111
rect = qImage.rect()
110112
p = QtGui.QPainter(self)
@@ -142,7 +144,9 @@ def paintEvent(self, e):
142144
stringBuffer = reg.to_string_argb()
143145
qImage = QtGui.QImage(stringBuffer, w, h,
144146
QtGui.QImage.Format_ARGB32)
145-
qImage.setDevicePixelRatio(self._dpi_ratio)
147+
if hasattr(qImage, 'setDevicePixelRatio'):
148+
# Not available on Qt4 or some older Qt5.
149+
qImage.setDevicePixelRatio(self._dpi_ratio)
146150
# Adjust the stringBuffer reference count to work
147151
# around a memory leak bug in QImage() under PySide on
148152
# Python 3.x
@@ -238,7 +242,6 @@ def __init__(self, figure):
238242
super(FigureCanvasQTAgg, self).__init__(figure=figure)
239243
self._drawRect = None
240244
self.blitbox = []
241-
self._dpi_ratio = self.devicePixelRatio()
242245
# We don't want to scale up the figure DPI more than once.
243246
# Note, we don't handle a signal for changing DPI yet.
244247
if not hasattr(self.figure, '_original_dpi'):

0 commit comments

Comments
 (0)