From cd302a1df45727e1768191580b8c7faeda7817cf Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 6 Jun 2020 00:52:39 -0400 Subject: [PATCH] Backport PR #17565: FIX: support Qt 5.15 Merge pull request #17565 from tacaswell/fix_new_qt FIX: support Qt 5.15 Conflicts: lib/matplotlib/backends/backend_qt5.py - code was very different, int cast done differently. --- lib/matplotlib/backends/backend_qt5.py | 2 +- lib/matplotlib/backends/backend_qt5agg.py | 79 ++++++++++++----------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index f3dcf1c513a8..718c005f1f0d 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -508,7 +508,7 @@ def _draw_rect_callback(painter): pen = QtGui.QPen(QtCore.Qt.black, 1 / self._dpi_ratio, QtCore.Qt.DotLine) painter.setPen(pen) - painter.drawRect(*(pt / self._dpi_ratio for pt in rect)) + painter.drawRect(*(int(pt / self._dpi_ratio) for pt in rect)) else: def _draw_rect_callback(painter): return diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 6e90dc8a88ff..09a2a261844d 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -38,45 +38,46 @@ def paintEvent(self, event): return painter = QtGui.QPainter(self) - - # See documentation of QRect: bottom() and right() are off by 1, so use - # left() + width() and top() + height(). - rect = event.rect() - # scale rect dimensions using the screen dpi ratio to get - # correct values for the Figure coordinates (rather than QT5's coords) - width = rect.width() * self._dpi_ratio - height = rect.height() * self._dpi_ratio - left, top = self.mouseEventCoords(rect.topLeft()) - # shift the "top" by the height of the image to get the - # correct corner for our coordinate system - bottom = top - height - # same with the right side of the image - right = left + width - # create a buffer using the image bounding box - bbox = Bbox([[left, bottom], [right, top]]) - reg = self.copy_from_bbox(bbox) - buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( - memoryview(reg)) - - # clear the widget canvas - painter.eraseRect(rect) - - qimage = QtGui.QImage(buf, buf.shape[1], buf.shape[0], - QtGui.QImage.Format_ARGB32_Premultiplied) - if hasattr(qimage, 'setDevicePixelRatio'): - # Not available on Qt4 or some older Qt5. - qimage.setDevicePixelRatio(self._dpi_ratio) - # set origin using original QT coordinates - origin = QtCore.QPoint(rect.left(), rect.top()) - painter.drawImage(origin, qimage) - # Adjust the buf reference count to work around a memory - # leak bug in QImage under PySide on Python 3. - if QT_API in ('PySide', 'PySide2'): - ctypes.c_long.from_address(id(buf)).value = 1 - - self._draw_rect_callback(painter) - - painter.end() + try: + # See documentation of QRect: bottom() and right() are off + # by 1, so use left() + width() and top() + height(). + rect = event.rect() + # scale rect dimensions using the screen dpi ratio to get + # correct values for the Figure coordinates (rather than + # QT5's coords) + width = rect.width() * self._dpi_ratio + height = rect.height() * self._dpi_ratio + left, top = self.mouseEventCoords(rect.topLeft()) + # shift the "top" by the height of the image to get the + # correct corner for our coordinate system + bottom = top - height + # same with the right side of the image + right = left + width + # create a buffer using the image bounding box + bbox = Bbox([[left, bottom], [right, top]]) + reg = self.copy_from_bbox(bbox) + buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( + memoryview(reg)) + + # clear the widget canvas + painter.eraseRect(rect) + + qimage = QtGui.QImage(buf, buf.shape[1], buf.shape[0], + QtGui.QImage.Format_ARGB32_Premultiplied) + if hasattr(qimage, 'setDevicePixelRatio'): + # Not available on Qt4 or some older Qt5. + qimage.setDevicePixelRatio(self._dpi_ratio) + # set origin using original QT coordinates + origin = QtCore.QPoint(rect.left(), rect.top()) + painter.drawImage(origin, qimage) + # Adjust the buf reference count to work around a memory + # leak bug in QImage under PySide on Python 3. + if QT_API in ('PySide', 'PySide2'): + ctypes.c_long.from_address(id(buf)).value = 1 + + self._draw_rect_callback(painter) + finally: + painter.end() def blit(self, bbox=None): # docstring inherited