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
from PyQt5 import sip
。 - Presbiteroimport 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.')
destroyed()
дҝЎеҸ·жӣҙе®№жҳ“зҡ„ж–№жі•пјҢеӣ жӯӨжҲ‘жҖҖз–‘еңЁPyQtдёӯжҳҜеҗҰеҸҜиғҪе®һзҺ°гҖӮ - smerlin