如果未分配给 PySide 中的对象变量,对象似乎会被删除 [英] Objects seems to be deleted if not assigned to object variable in PySide

查看:55
本文介绍了如果未分配给 PySide 中的对象变量,对象似乎会被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QStandardItemModel 来表示数据的层次结构,但是当我将 QStandardItems 添加到模型时,我必须将它们分配到对象成员变量中,否则对象似乎被删除了.

I'm trying to use QStandardItemModel to represent a hierarchy of data, but when I'm adding QStandardItems to the model, I have to assign them in object member variables, or the objects seems to be deleted.

例如

self.tree_model = QStandardItemModel()
self.tree_model.setHorizontalHeaderLabels(['Category'])
self.out_insertions = QStandardItem("Insertions")
self.tree_model.invisibleRootItem().appendRow(self.out_insertions)

按预期工作(在类别"列下插入插入"行).但是,如果我删除 self.out_insertion 分配,例如:

Works as expected (an "Insertion" row is inserted under the column "Category"). But if I remove the self.out_insertion assignment, like:

self.tree_model = QStandardItemModel()
self.tree_model.setHorizontalHeaderLabels(['Category'])
self.tree_model.invisibleRootItem().appendRow(QStandardItem("Insertions"))

它不起作用(显示一个空行).

It doesn't work (an empty row is shown).

我使用的是 Qt 4.6.3 和 PySide 0.4.1.有人能解释一下为什么会这样吗?

I'm using Qt 4.6.3 and PySide 0.4.1. Can someone explain me why this happens?

提前致谢

~阿基

推荐答案

你的对象被垃圾回收,因为不再存在对它的 (Python) 引用.

Your object get garbage collected since no more (Python) references to it exist.

此行为在事情中有所描述在 PyQt 文档中知道'.

大多数这些问题(在 PyQt 领域)都可以通过正确的parenting来避免(这使得 Qt 拥有所有权而不是 PyQt).

Most of these problems (in PyQt land) can be avoided by correct parenting (which makes Qt take ownership instead of PyQt).

这篇关于如果未分配给 PySide 中的对象变量,对象似乎会被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆