本文介绍了是否可以查询 PyQt4 QObject 以确定底层 C++ 实例是否已被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以为 QObject 捕获 destroyed() 信号,但我想简单地测试一下 Python 对象是否仍然引用有效的 C++ Qt 对象.有没有直接这样做的方法?

The destroyed() signal can be trapped for a QObject, but I would like to simply test if the Python object still references a valid C++ Qt object. Is there a method for doing so directly?

推荐答案

如果你导入了 sip 模块,你可以调用它的 .isdeleted 函数.

If you import the sip module you can call its .isdeleted function.

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

这篇关于是否可以查询 PyQt4 QObject 以确定底层 C++ 实例是否已被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:26