Is it possible to query QQbject PyQt4 to determine if the underlying C ++ instance has been corrupted?

The destroy () signal can be trapped for a QObject, but I would just like to check if the Python object supports a valid C ++ Qt object. Is there any way to do this directly?

+7
python qt pyqt pyqt4
source share
2 answers

If you import the sip module, you can call it .isdeleted.

import sip from PyQt4.QtCore import QObject q = QObject() sip.isdeleted(q) False sip.delete(q) q <PyQt4.QtCore.QObject object at 0x017CCA98> q.isdeleted(q) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted 
+15
source share

You can use the WeakRef class in the Python standard library. It would look like this:

 import weakref q = QObject() w = weakref.ref(q) if w() is not None: # Remember the parentheses! print('The QObject is still alive.') else: print('Looks like the QObject died.') 
+2
source share

All Articles