-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: AttributeError: 'QResizeEvent' object has no attribute 'pos' #22409
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
Comments
I cannot reproduce the problem. It also seems strange that you get a QResizeEvent in enterEvent? |
Yes, it seems strange to me too. I attach a small video with the procedure to reproduce the error. test.mp4Mario S. |
That's strange :/ (but I'd think blindly ignoring the error with a try... except is wrong too, as something weird is going on). |
I've got the same issue. Do you have any idea about how to avoid this before issue fixed? |
Please provide your repro. Also include the environment, all relevant versions of modules, installation methods (conda/pip/linux package), etc. |
I ran into the same traceback using Matplotlib 3.5.3 and PyQt 5.15.6. Here is the minimal (admittedly, quite non-minimal) example, run it, click on the second tab, then move the mouse over FigureCanvas:
I did some digging and the culprit seems to be frame = sys._getframe() in FigureCanvas' resizeEvent method, which causes a memory leak, which in turn leads to the wrong event (QResizeEvent) being sent to enterEvent. Sadly, I didn't dig further than that - I have no idea why this only rarely leads to an exception - the leak is pretty consistent. Also, the exception doesn't happen using PyQt6. If I had to guess I'd say there's a race condition somewhere. I wasn't able to reproduce the problem originally reported here, so I cannot be certain the root cause is the same, but I think it's safe enough to assume it is. For anyone else facing this issue, the workaround I've used is:
Not the prettiest solution, and it does come with a slight performance penalty, but at least it manages to avoid the exception. By explicitly forcing garbage collection, we ensure frame variable is cleaned up before any other Qt code is executed. A faster option for someone who doesn't care about enter/leave events would be to override enterEvent and leaveEvent and simply ignore them. Filtering out events in enterEvent by type is not an option, since when this occurs, all subsequent enter events also receive the same QResizeEvent. The solution I would suggest is quite simple, I would explicitly delete the variable frame once it's no longer needed in FigureCanvas' resizeEvent method:
|
Perhaps the cleaner way to check for recursion would be something like def resizeEvent(self, event):
if self._in_resize_event: return # where the attribute is initialized to False in init.
self._in_resize_event = True
try: ... # the actual resize logic
finally: self._in_resize_event = False ? |
Sounds good. Should I create a PR or will you handle it? |
@anthepro If you are willing to open a PR that would be great! |
Closed by #23729, I guess. |
Bug summary
This bug is similar to #11607. In my case, I get it when I combine PyQt5 and matplotlib 3.5.1. In any of the other combinations (PyQt5 and matplotlib==3.4.3 or PyQt6 and matplotlib==3.5.1) it works as expected.
Code for reproduction
Actual outcome
To reproduce:
When the subwindow is maximized I get this error:
Expected outcome
The maximized subwindow
Additional information
Using
matplotlib==3.5.1
withPyQt5
. Not happen with other combinationsmatplotlib==3.5.1
andPyQt6
ormatplotlib==3.4.3
andPyQt5
This bug is related to the QEvents methods. QResizeEvent does have not any method to get the position in PyQt5, unlike PyQt6 which does have it
Add a try-except clausule to this method
matplotlib/lib/matplotlib/backends/backend_qt.py
Lines 264 to 266 in 60d0c33
After this, work as expected!
Happy to open a PR if it is needed!
Operating system
Linux Mint 20.2
Matplotlib Version
3.5.1
Matplotlib Backend
QtAgg
Python version
Python 3.8.5
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: