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

Skip to content

Commit 02f8523

Browse files
piannucciQuLogic
authored andcommitted
Initial HiDPI support for qt5agg backend
1 parent 08eccfb commit 02f8523

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
@@ -250,10 +250,14 @@ def leaveEvent(self, event):
250250
QtWidgets.QApplication.restoreOverrideCursor()
251251
FigureCanvasBase.leave_notify_event(self, guiEvent=event)
252252

253+
def mouseEventCoords(self, pos):
254+
x = pos.x() * self.devicePixelRatio()
255+
# flip y so y=0 is bottom of canvas
256+
y = self.figure.bbox.height - pos.y() * self.devicePixelRatio()
257+
return x, y
258+
253259
def mousePressEvent(self, event):
254-
x = event.pos().x()
255-
# flipy so y=0 is bottom of canvas
256-
y = self.figure.bbox.height - event.pos().y()
260+
x, y = self.mouseEventCoords(event.pos())
257261
button = self.buttond.get(event.button())
258262
if button is not None:
259263
FigureCanvasBase.button_press_event(self, x, y, button,
@@ -262,9 +266,7 @@ def mousePressEvent(self, event):
262266
print('button pressed:', event.button())
263267

264268
def mouseDoubleClickEvent(self, event):
265-
x = event.pos().x()
266-
# flipy so y=0 is bottom of canvas
267-
y = self.figure.bbox.height - event.pos().y()
269+
x, y = self.mouseEventCoords(event.pos())
268270
button = self.buttond.get(event.button())
269271
if button is not None:
270272
FigureCanvasBase.button_press_event(self, x, y,
@@ -274,16 +276,12 @@ def mouseDoubleClickEvent(self, event):
274276
print('button doubleclicked:', event.button())
275277

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

283283
def mouseReleaseEvent(self, event):
284-
x = event.x()
285-
# flipy so y=0 is bottom of canvas
286-
y = self.figure.bbox.height - event.y()
284+
x, y = self.mouseEventCoords(event)
287285
button = self.buttond.get(event.button())
288286
if button is not None:
289287
FigureCanvasBase.button_release_event(self, x, y, button,
@@ -292,9 +290,7 @@ def mouseReleaseEvent(self, event):
292290
print('button released')
293291

294292
def wheelEvent(self, event):
295-
x = event.x()
296-
# flipy so y=0 is bottom of canvas
297-
y = self.figure.bbox.height - event.y()
293+
x, y = self.mouseEventCoords(event)
298294
# from QWheelEvent::delta doc
299295
if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
300296
steps = event.angleDelta().y() / 120
@@ -324,8 +320,9 @@ def keyReleaseEvent(self, event):
324320
print('key release', key)
325321

326322
def resizeEvent(self, event):
327-
w = event.size().width()
328-
h = event.size().height()
323+
dpi_ratio = getattr(self, '_dpi_ratio', 1)
324+
w = event.size().width() * dpi_ratio
325+
h = event.size().height() * dpi_ratio
329326
if DEBUG:
330327
print('resize (%d x %d)' % (w, h))
331328
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
@@ -101,6 +101,7 @@ def paintEvent(self, e):
101101
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
102102
self.renderer.height,
103103
QtGui.QImage.Format_ARGB32)
104+
qImage.setDevicePixelRatio(self._dpi_ratio)
104105
# get the rectangle for the image
105106
rect = qImage.rect()
106107
p = QtGui.QPainter(self)
@@ -136,6 +137,7 @@ def paintEvent(self, e):
136137
stringBuffer = reg.to_string_argb()
137138
qImage = QtGui.QImage(stringBuffer, w, h,
138139
QtGui.QImage.Format_ARGB32)
140+
qImage.setDevicePixelRatio(self._dpi_ratio)
139141
# Adjust the stringBuffer reference count to work
140142
# around a memory leak bug in QImage() under PySide on
141143
# Python 3.x
@@ -226,6 +228,7 @@ def __init__(self, figure):
226228
super(FigureCanvasQTAgg, self).__init__(figure=figure)
227229
self._drawRect = None
228230
self.blitbox = []
231+
self._dpi_ratio = self.devicePixelRatio()
229232
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
230233

231234

0 commit comments

Comments
 (0)