[PyQt] Cleanup on object destruction

AnyOldName3 krizdjali at gmail.com
Mon Jun 10 16:51:43 BST 2019

I've made a Python class that extends QOpenGLWidget, and needs to clean up
some OpenGL objects when it's destroyed. The Qt documentation for this says
cleanup code should be called by the destructor and when the
QOpenGLContext::aboutToBeDestroyed signal is emitted, so I've tried calling
my cleanup method from __del__ and connecting it as a slot to the signal.
Unfortunately, the signal isn't emitted when the widget is destroyed, just
when the widget has its context replaced, and __del__ seems to be called too
late as calling some of the C++ methods doesn't work (no exception is
thrown, but nothing after the call is executed).
The next approach I tried was connecting the cleanup method to the widget's
destroyed signal, which required a lambda as mentioned  here
. This signal isn't emitted until after __del__ has been called, and so I
get exceptions due to the C++ object no longer existing.
The only approach I've had success with is getting the widget's parent and
connecting to /its/ destroyed signal, but this doesn't seem especially
robust - I can imagine situations where a hypothetical widget wouldn't
necessarily have a parent when it was destroyed, but may well still need to
do cleanup.
Is there a nicer way of doing this, for example, a method that SIP will call
as the first thing the C++ destructor does, so that nothing's been destroyed
yet? If not, could something like that be added?

Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190610/443dfa50/attachment.html>

More information about the PyQt mailing list