python – 是否可以查询PyQt4 QObject以确定底层C实例是否已被销毁?

destroyed()信号可以被捕获用于QObject,但我想简单地测试Python对象是否仍然引用有效的C Qt对象.有没有直接这样做的方法?
最佳答案
如果导入sip模块,则可以调用其.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

转载注明原文:python – 是否可以查询PyQt4 QObject以确定底层C实例是否已被销毁? - 代码日志