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

Skip to content

Commit 26befec

Browse files
committed
Initial HiDPI support for qt5agg backend
1 parent 390dab3 commit 26befec

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,14 @@ def leaveEvent(self, event):
249249
QtWidgets.QApplication.restoreOverrideCursor()
250250
FigureCanvasBase.leave_notify_event(self, guiEvent=event)
251251

252+
def mouseEventCoords(self, pos):
253+
x = pos.x() * self.devicePixelRatio()
254+
# flip y so y=0 is bottom of canvas
255+
y = self.figure.bbox.height - pos.y() * self.devicePixelRatio()
256+
return x, y
257+
252258
def mousePressEvent(self, event):
253-
x = event.pos().x()
254-
# flipy so y=0 is bottom of canvas
255-
y = self.figure.bbox.height - event.pos().y()
259+
x, y = self.mouseEventCoords(event.pos())
256260
button = self.buttond.get(event.button())
257261
if button is not None:
258262
FigureCanvasBase.button_press_event(self, x, y, button,
@@ -261,9 +265,7 @@ def mousePressEvent(self, event):
261265
print('button pressed:', event.button())
262266

263267
def mouseDoubleClickEvent(self, event):
264-
x = event.pos().x()
265-
# flipy so y=0 is bottom of canvas
266-
y = self.figure.bbox.height - event.pos().y()
268+
x, y = self.mouseEventCoords(event.pos())
267269
button = self.buttond.get(event.button())
268270
if button is not None:
269271
FigureCanvasBase.button_press_event(self, x, y,
@@ -273,16 +275,12 @@ def mouseDoubleClickEvent(self, event):
273275
print('button doubleclicked:', event.button())
274276

275277
def mouseMoveEvent(self, event):
276-
x = event.x()
277-
# flipy so y=0 is bottom of canvas
278-
y = self.figure.bbox.height - event.y()
278+
x, y = self.mouseEventCoords(event)
279279
FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=event)
280280
# if DEBUG: print('mouse move')
281281

282282
def mouseReleaseEvent(self, event):
283-
x = event.x()
284-
# flipy so y=0 is bottom of canvas
285-
y = self.figure.bbox.height - event.y()
283+
x, y = self.mouseEventCoords(event)
286284
button = self.buttond.get(event.button())
287285
if button is not None:
288286
FigureCanvasBase.button_release_event(self, x, y, button,
@@ -291,9 +289,7 @@ def mouseReleaseEvent(self, event):
291289
print('button released')
292290

293291
def wheelEvent(self, event):
294-
x = event.x()
295-
# flipy so y=0 is bottom of canvas
296-
y = self.figure.bbox.height - event.y()
292+
x, y = self.mouseEventCoords(event)
297293
# from QWheelEvent::delta doc
298294
if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
299295
steps = event.angleDelta().y() / 120
@@ -323,8 +319,9 @@ def keyReleaseEvent(self, event):
323319
print('key release', key)
324320

325321
def resizeEvent(self, event):
326-
w = event.size().width()
327-
h = event.size().height()
322+
dpi_ratio = getattr(self, '_dpi_ratio', 1)
323+
w = event.size().width() * dpi_ratio
324+
h = event.size().height() * dpi_ratio
328325
if DEBUG:
329326
print('resize (%d x %d)' % (w, h))
330327
print("FigureCanvasQt.resizeEvent(%d, %d)" % (w, h))

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def paintEvent(self, e):
100100
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
101101
self.renderer.height,
102102
QtGui.QImage.Format_ARGB32)
103+
qImage.setDevicePixelRatio(self._dpi_ratio)
103104
# get the rectangle for the image
104105
rect = qImage.rect()
105106
p = QtGui.QPainter(self)
@@ -135,6 +136,7 @@ def paintEvent(self, e):
135136
stringBuffer = reg.to_string_argb()
136137
qImage = QtGui.QImage(stringBuffer, w, h,
137138
QtGui.QImage.Format_ARGB32)
139+
qImage.setDevicePixelRatio(self._dpi_ratio)
138140
# Adjust the stringBuffer reference count to work
139141
# around a memory leak bug in QImage() under PySide on
140142
# Python 3.x
@@ -222,6 +224,7 @@ def __init__(self, figure):
222224
super(FigureCanvasQTAgg, self).__init__(figure=figure)
223225
self._drawRect = None
224226
self.blitbox = []
227+
self._dpi_ratio = self.devicePixelRatio()
225228
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
226229

227230

0 commit comments

Comments
 (0)