[PyQt] QTreeWidgetItem.addChild() no longer keeping references

Phil Thompson phil at riverbankcomputing.com
Tue Mar 5 12:36:25 GMT 2013


On Mon, 4 Mar 2013 23:22:04 -0500, Luke Campagnola
<lcampagn at email.unc.edu>
wrote:
> Howdy,
> I have found that somewhere between 4.8.3 and 4.9.6, calling
> QTreeWidgetItem.addChild(item) no longer creates a reference to the
> specified item (or, at least that is my best guess as to what's going
on).
> I noticed this because I often assign custom attributes to these items
and
> found that the attributes go missing under 4.9.6 if I don't keep an
> explicit reference to the item.
> 
> Here's an example from 4.8.3:
> 
>>>> from PyQt4.QtGui import *
>>>> app = QApplication([])
>>>> tree = QTreeWidget()
>>>> item = QTreeWidgetItem(["Item"])
>>>> item.attr = "test"
>>>> tree.invisibleRootItem().addChild(item)
>>>> del item
>>>> import gc
>>>> gc.collect()
> 0
>>>> tree.topLevelItem(0).attr
> 'test'
> 
> ..and the same under 4.9.6:
> 
>>>> from PyQt4.QtGui import *
>>>> app = QApplication([])
>>>> tree = QTreeWidget()
>>>> item = QTreeWidgetItem(["Item"])
>>>> item.attr = "test"
>>>> tree.invisibleRootItem().addChild(item)
>>>> del item
>>>> import gc
>>>> gc.collect()
> 0
>>>> tree.topLevelItem(0).attr
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'QTreeWidgetItem' object has no attribute 'attr'
> 
> Questions are:
> 1) Is this a bug, or was the change deliberate?
> 2) Does this apply only to QTreeWidgetItem, or are there similar changes
> elsewhere I should watch out for?

I think it's an old bug (specifically with invisibleRootItem()) exposed by
a recent SIP fix. Fixed in tonight's PyQt snapshot.

Thanks,
Phil


More information about the PyQt mailing list