- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
QGraphicsItem
状态似乎从 To be destroy by: C/C++
变为 To be destroy 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。而且它仍然存在。为什么?
最佳答案
当你 add the item to the scene, Qt takes ownership of it ,因此您不需要在 Python 端保留对它的显式引用。这意味着当场景被删除时,该项目的 C++ 部分也将被删除 - 如果没有其他对 Python 部分的引用,它也将被垃圾收集(因此不会留下任何东西)。
当你 remove the item from the scene, ownership passes back to the caller ,现在对删除它负有全部责任。如果QGraphicsRectItem
继承了QObject
,您可以调用deleteLater()
来删除C++部分 - 但这仍然会给您留下空的Python包装器,所以实际上并没有多大区别。
删除 PyQt 包装器的方法与任何其他 Python 对象相同 - 即使用 del
,或者简单地让它超出范围并允许它在正常情况下被垃圾收集方式。没有特殊的 PyQt 方法来删除此类对象,因为不需要任何特殊的方法。
在您的示例中,您实际上从未尝试删除该项目,因此 sip 报告的信息完全符合预期。
最后,请注意,始终可以独立于 PyQt 包装器删除 C++ 部分。如果您这样做,然后尝试访问该项目的方法,您将收到 RuntimeError
:
>>> scene = QGraphicsScene()
>>> node = QGraphicsRectItem()
>>> scene.addItem(node)
>>> del scene
>>> node.rect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: wrapped C/C++ object of type QGraphicsRectItem has been deleted
更新:
下面是一个演示,展示了当项目从场景中删除并允许超出范围时如何对其进行垃圾收集。当然,如果在其他地方有对该项目的任何引用(即,如果引用计数 > 1),则该项目将不会被删除。
import sip, gc
from PyQt5 import QtCore, QtWidgets
app = QtWidgets.QApplication(['test'])
scene = QtWidgets.QGraphicsScene()
def test():
node = QtWidgets.QGraphicsRectItem()
scene.addItem(node)
scene.removeItem(node)
print("Deleted ? ", sip.isdeleted(node))
print("Owned by Python ? ", sip.ispyowned(node))
sip.dump(node)
print()
test()
print('gc collect...')
gc.collect()
for obj in gc.get_objects():
if isinstance(obj, QtWidgets.QGraphicsRectItem):
print(obj)
break
else:
print('deleted')
输出:
Deleted ? False
Owned by Python ? True
<PyQt5.QtWidgets.QGraphicsRectItem object at 0x7f90f66d5798>
Reference count: 3
Address of wrapped object: 0x205bd80
Created by: Python
To be destroyed by: Python
Parent wrapper: NULL
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
gc collect...
deleted
关于python - PyQt 中的 QGraphicsItem 什么时候被销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51392167/
当你有一个对象 OBJ1 包含一个关键部分 CS 和一个指向另一个对象 OBJ2 的指针时,我无法弄清楚什么是正确的关闭过程 假设您有两个函数 A 和 B。 A进入临界区,修改OBJ2中的数据后离开临
我有一个成员变量声明为 CComPtr m_spXMLDoc; XML 文档是这样创建的 CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC
在我的工作平台中,我遇到了 session_destroy 问题 function logout() { $_SESSION['id'] = ''; session_destroy(); } 在这
如何使用 destroy 删除 jScrollPane。请您给出以下代码的简单示例: $(document).ready(function() { $(".div1").jScrollPane
这是我在这里发表的第一篇文章,但我想对社区表示感谢,因为通过来到这里并在已经得到解答的问题中找到解决方案,我已经无数次找到了问题的解决方案。 话虽这么说,我想进入正题。我正在 Code Gear 的
我在导航 View 中工作。在此导航 View 中,您可以偶然发现个人资料页面。在此个人资料页面中,您可以看到与当前个人资料相关的其他个人资料(基本上是数据 View 中显示的图片)。您可以通过点击这
我想销毁项目中的 session ,这样当我单击“注销”时,它会转到页面“KillSession.jsp”,在该文件中我编写了“session.invalidate();”然后我将用户重定向到登录页面
我有关于 Thread 的 2 问题,我只是想澄清一些事情。使用以下代码: public class MyThread implements Runnable { Boolean StopTh
我遇到了内存泄漏,代码类似于下面的代码(这是每个循环都有不同输入的模拟)。 问题 对象 Object_XXX 非常复杂,它与数据库以及其他填充了数据库数据的对象有连接。 for(int i=0
当我在 Python 中启动一个类时,我给它一些值。然后我调用类中执行某些操作的方法。这是一个片段: class TestClass(): def __init__(self):
我想删除所有 div、类、属性和几乎所有 CKEDITOR 添加到 DOM 的内容。例如调用 jquery tabs("destroy");将删除所有由 jQuery 选项卡添加的 div。我怎样才能
我想清除析构函数中的一个映射,但我不知道它是否为空。如果我按如下方式清除它可以吗? for(std::map::iterator it = m_map.begin(); it != m_map.end
我正在尝试克隆 TikTok 应用。对于主屏幕,我制作了一个 VerticalViewPager(自定义 View 分页器),其中包含“点赞”按钮、标题和评论。我正在从 Firebase 检索视频。
我正在制作自己的游戏。目标之一是在世界中拥有尽可能多的物体。在这个游戏中,需要在一些不可预测的时间段内创建许多对象(比如武器开火会创建一个对象),一旦该弹丸击中某物,该对象也需要被摧毁(也许它击中的东
有没有办法在 JavaScript 中破坏 HTML5 WebWorker? 这是我的情况:我有一个 Web 应用程序生成相当数量的 WebWorker(在 16 到 32 之间的任何地方)来优化一些
如何销毁 php 中的 session ? 事情是当用户点击注销按钮时, session 将结束,他将被重定向到 index.php 这是我的代码 客户.php 这是来自用户想要再次登录的 ind
关于 GtkBuilder 的问题。 当我们取消引用构建器指针时,它是否会破坏构建器创建的所有屏幕/小部件? if( builder_ptr ) g_object_unref(G_OBJECT
有没有办法销毁 WebView 实例?如果页面加载,并说视频开始播放,我希望能够,当我隐藏 WebView 时,基本上可以销毁它,或者至少重置它。 我知道我可以听 visibleProperty 并执
我有一组可拖动的元素。如何删除可拖动功能? $('.draggable').draggable('disable') 在我的情况下不是一个选项 $('.draggable').draggable('d
下面的代码会抛出一个 EZDecompressionError 消息 'Invalid ZStream operation' 每当行 Reader.Free 被执行。有人可以告诉我这段代码有什么问题吗
我是一名优秀的程序员,十分优秀!