1

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

例如

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 分配,例如:

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

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

我正在使用 Qt 4.6.3 和 PySide 0.4.1。有人能解释一下为什么会这样吗?

提前致谢

~阿基

4

1 回答 1

4

您的对象会被垃圾收集,因为不再存在(Python)对它的引用。

PyQt 文档中的“注意事项”中描述了这种行为。

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

于 2010-11-01T18:12:55.317 回答