【发布时间】:2018-07-18 01:13:31
【问题描述】:
似乎QGraphicsItem 状态从To be destroyed by: C/C++ 变为To be destroyed by: Python,虽然没有被破坏并且仍然可以访问。这是预期的行为吗?如果是这样,有人可以解释一下吗?
创作部分
node = QGraphicsRectItem()
self.scene.addItem(node)
print("Deleted ? ", sip.isdeleted(node))
print("Owned by Python ? ", sip.ispyowned(node))
sip.dump(node)
输出
Deleted ? False
Owned by Python ? False
<PyQt5.QtWidgets.QGraphicsRectItem object at 0x7fcdb82371f8>
Reference count: 3
Address of wrapped object: 0x214bf80
Created by: Python
To be destroyed by: C/C++
Parent wrapper: <PyQt5.QtWidgets.QGraphicsScene object at 0x7fcdb821ea68>
Next sibling wrapper: <__main__.Host object at 0x7fcdb8237dc8>
Previous sibling wrapper: NULL
First child wrapper: NULL
删除部分
self.scene.removeItem(node)
print("Deleted ? ", sip.isdeleted(node))
print("Owned by Python ? ", sip.ispyowned(node))
sip.dump(node)
输出
Deleted ? False
Owned by Python ? True
<PyQt5.QtWidgets.QGraphicsRectItem object at 0x7fcdb82371f8>
Reference count: 2
Address of wrapped object: 0x214bf80
Created by: Python
To be destroyed by: Python
Parent wrapper: NULL
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
可以看到删除后它现在归 Python 所有。它仍然存在。为什么?
【问题讨论】:
-
您是否希望使用 removeItem () 将项目从内存中删除?如果是这样,removeItem() 不会从内存中删除该项目,其目标是在另一个场景中重用该项目,以便程序员负责管理该内存,这在 Qt 中也会发生。类似的行为有 QListWidgetItem。
-
@eyllanesc 同意。我不希望那样。关键是,使用
PyQt,我没有显式析构函数或delete指令,与C++ 一样。那我必须手动强制吗?如果是这样,最好的 PyQt'ish 方法是什么,而不是使用 sip 或 python 的其他解决方案(如del)?
标签: python pyqt pyqt5 destroy qgraphicsitem