Skip to content
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

Differences in QEvent subclass APIs in PyQt6 cause attribute and/or type errors #394

Closed
ArthurGW opened this issue Dec 19, 2022 · 1 comment · Fixed by #408
Closed

Differences in QEvent subclass APIs in PyQt6 cause attribute and/or type errors #394

ArthurGW opened this issue Dec 19, 2022 · 1 comment · Fixed by #408

Comments

@ArthurGW
Copy link

For example, my application uses qtconsole, which in turns uses QtPy. At one point, qtconsole does:

        elif etype == QtCore.QEvent.MouseMove:
            anchor = self._control.anchorAt(event.pos())
            QtWidgets.QToolTip.showText(event.globalPos(), anchor)

However, when running PyQt6, the method QMouseEvent.globalPos() has been removed (as it has also been removed in Qt6 itself). Simply changing to call QMouseEvent.globalPosition() doesn't work, as the return type of that is different, QPoint (old) v.s. QPointF (new).

This is particularly annoying as most event classes used to have the same API - pos and globalPos returning QPoint, sometimes posF etc. for a floating version. Now in Python you have to do a load of type checks when you have a method that handles multiple event types. In C++ this would be spotted at compile time, which is probably why the Qt devs thought it was a reasonable change to make.

I would propose mapping this method similar to how e.g. QFontMetrics.width is, by monkey patching it directly onto the class object after import. i.e.:

QMouseEvent.globalPos = lambda self: self.globalPosition().toPoint()

There may be other missing methods, for example QMouseEvent.pos() doesn't actually exist in Qt6 but is defined in PyQt6 anyway - I don't know whether it exists in PySide6.

@dalthviz dalthviz added this to the v2.4.0 milestone Dec 19, 2022
@dalthviz
Copy link
Member

Hi @ArthurGW thank you for the feedback! That sounds like a good idea 👍 If you want to help us implementing the mappings let us know!

StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 13, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 17, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 17, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 17, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 17, 2023
@dalthviz dalthviz linked a pull request Feb 22, 2023 that will close this issue
@dalthviz dalthviz modified the milestones: v2.4.0, v2.3.1 Feb 22, 2023
@dalthviz dalthviz assigned dalthviz and unassigned dalthviz Feb 22, 2023
CAM-Gerlach added a commit that referenced this issue Feb 23, 2023
* Might close #394
* Rephrase a comment, as @CAM-Gerlach suggested
* Test the fix for #394
* Format the docstring as suggested by @CAM-Gerlach
* Ensure the created window is of sufficient size to point at
* Don't wait before moving and clicking the mouse.
  `QMainWindow.show()` finishes when the window appears. So, no extra waiting needed.
* Don't close the window at the end

Co-authored-by: C.A.M. Gerlach <[email protected]>
StSav012 added a commit to StSav012/qtpy that referenced this issue Feb 23, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Mar 23, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Mar 24, 2023
StSav012 added a commit to StSav012/qtpy that referenced this issue Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants