Skip to content

Add backend for Qt6 (support for PyQt6, PySide6) #19674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

mfitzp
Copy link
Member

@mfitzp mfitzp commented Mar 9, 2021

PR Summary

This PR adds support for Qt6, with support for both PyQt6 and PySide6 libraries.

For the most part PySide6 is source-compatible with PySide2, aside from the imports and some small changes (see below). PyQt6 is quite different. The main changes that affect us here are

  • Enums are no longer available at the top level of modules/objects. For example, previously Qt.GlobalColor.white was previously available as Qt.white. This short style was present in PyQt5 & PySide2 and is still supported in PySide6. PySide6 also supports the longer version (but...)
  • While enum types are identical between PyQt6 & PySide6, flag types have different names. So Qt.GlobalColor.white (enum) is the same in both. But alignment flags are in Qt.Alignment.AlignLeft in PyQt6 and Qt.AlignmentFlag.AlignLeft in PySide6.
  • (this may be a bug) Mouse events in PyQt6 no longer have the .pos() attribute. We can use .position() instead.
  • PyQt6 no longer supports using exec_(), must use exec(). PySide6 is exactly the reverse.
  • Qt.MidButton is now Qt.MiddleButton

The code to work around these differences is in qt_compat.py. Because of the differences in flag naming, I've opted to just copy the entries from the enums/flags to the parent object (matching PyQt5/PySide2). There is a further shim for QDialog to account for PyQt no longer having exec_().

Since this adds new files, the diff doesn't show the changes. Below is a diff between v5 and v6.

backends (qt6-backend) $ diff backend_qt5.py backend_qt6.py
158c158
<                     "PyQt4", "sip", "PySide", "PySide2", "Shiboken"])
---
>                     "PyQt4", "sip", "PySide", "PySide2", "PySide6", "Shiboken"])
205c205
<                QtCore.Qt.MidButton: MouseButton.MIDDLE,
---
>                QtCore.Qt.MiddleButton: MouseButton.MIDDLE,
279c279
<             x, y = self.mouseEventCoords(event.pos())
---
>             x, y = self.mouseEventCoords(event.position())
306c306
<         x, y = self.mouseEventCoords(event.pos())
---
>         x, y = self.mouseEventCoords(event.position())
313c313
<         x, y = self.mouseEventCoords(event.pos())
---
>         x, y = self.mouseEventCoords(event.position())
321c321
<         x, y = self.mouseEventCoords(event)
---
>         x, y = self.mouseEventCoords(event.position())
325c325
<         x, y = self.mouseEventCoords(event)
---
>         x, y = self.mouseEventCoords(event.position())
331,351c331,340
<     if QtCore.qVersion() >= "5.":
<         def wheelEvent(self, event):
<             x, y = self.mouseEventCoords(event)
<             # from QWheelEvent::delta doc
<             if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
<                 steps = event.angleDelta().y() / 120
<             else:
<                 steps = event.pixelDelta().y()
<             if steps:
<                 FigureCanvasBase.scroll_event(
<                     self, x, y, steps, guiEvent=event)
<     else:
<         def wheelEvent(self, event):
<             x = event.x()
<             # flipy so y=0 is bottom of canvas
<             y = self.figure.bbox.height - event.y()
<             # from QWheelEvent::delta doc
<             steps = event.delta() / 120
<             if event.orientation() == QtCore.Qt.Vertical:
<                 FigureCanvasBase.scroll_event(
<                     self, x, y, steps, guiEvent=event)
---
>     def wheelEvent(self, event):
>         x, y = self.mouseEventCoords(event.position())
>         # from QWheelEvent::delta doc
>         if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
>             steps = event.angleDelta().y() / 120
>         else:
>             steps = event.pixelDelta().y()
>         if steps:
>             FigureCanvasBase.scroll_event(
>                 self, x, y, steps, guiEvent=event)
488c477,480
<                 pen = QtGui.QPen(QtCore.Qt.black, 1 / self._dpi_ratio)
---
>                 pen = QtGui.QPen(
>                     QtCore.Qt.black,
>                     1 / self._dpi_ratio
>                 )
1015c1007
< class _BackendQT5(_Backend):
---
> class _BackendQT6(_Backend):

The differences being so minor, it may make sense to implement Qt6 based on Qt5 (or both vs a base).

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@jklymak
Copy link
Member

jklymak commented Mar 9, 2021

Thanks for this, however, there is already #19255

@mfitzp
Copy link
Member Author

mfitzp commented Mar 9, 2021

Thanks for this, however, there is already #19255

Oops. I'd searched + missed it. Will close

@mfitzp mfitzp closed this Mar 9, 2021
@anntzer
Copy link
Contributor

anntzer commented Mar 9, 2021

@mfitzp if you could by any chance help with investigating the CI crash on #19255 that would be super helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants