Skip to content

Fix 'GridItem.generatePicture' for cases of invalid size#3284

Merged
j9ac9k merged 1 commit intopyqtgraph:masterfrom
bbc131:fix_griditem
Mar 24, 2025
Merged

Fix 'GridItem.generatePicture' for cases of invalid size#3284
j9ac9k merged 1 commit intopyqtgraph:masterfrom
bbc131:fix_griditem

Conversation

@bbc131
Copy link
Copy Markdown
Contributor

@bbc131 bbc131 commented Mar 24, 2025

This commit fixes errors in GridItem that can occur when the widget is not visible or has an invalid size.

I encountered a
RuntimeWarning: divide by zero encountered in log10
in GridItem.generatePixmap at this line:

d = 10. ** np.floor(np.log10(np.abs(dist/nlTarget)) + 0.5)

However, I cannot reproduce this issue. But if the bounding rect is null (boundingRect() returns QtCore.QRectF()), dist becomes zero, triggering the warning.

While trying to reproduce this issue, I encountered another error:
AttributeError: 'NoneType' object has no attribute 'determinant'.
This can be reproduced by adding:

grid = pg.GridItem()
p1.addItem(grid)

to examples/InfiniteLabel.py at line 20. Running the example and resizing the window to its minimal possible size will trigger the error.

Both issues occur when the widget is not in a state where it can be safely painted.
This fix prevents drawing in such cases.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Mar 24, 2025

Hey @bbc131

Thanks for the PR; I have no issue with the diff; but I'm side-eyeing returning None from .deviceTransform(). I'm going back and forth on whether it would be better to return the device transform even if it is singular instead of making .deviceTransform return an optional.

@bbc131
Copy link
Copy Markdown
Contributor Author

bbc131 commented Mar 24, 2025

I'm not sure which approach would be better...

Always returning a QTransform seems like the right choice and is more consistent with Qt classes. However, it could also be more error-prone, if you're not familiar with these details. It's obvious that None shouldn't be used for anything. But it might be less clear that you need to check the QTransform object before inverting it.

@j9ac9k j9ac9k merged commit e39e530 into pyqtgraph:master Mar 24, 2025
36 checks passed
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.

2 participants