[Python] PySide QTreeWidget.Clear() causes crash

Hi,

I have three different QTreeWidgets, representing three different folder structures. When I try to clear them all three in a row the application crashes.

The only thing I do is:

self.treeWidget02.clear()
self.treeWidget01.clear()
self.treeWidget03.clear()

Sometimes it clears one and sometimes it clears two, then crashes. Sometimes it can clear them all without crashing. I have not been able to see any pattern too when it crashes and when not.

Have anyone had any similar problems and got a hunch what it could be? When I google it people speak about problem when threading, but I am a student and fairly new to python and pyside. So that is magic to me right now.

Edit1:
It also crashes if I try to do


widgets = [ self.treeWidget01, self.treeWidget02, self.treeWidget03 ]
for widget in widgets:
    aList = (range(widget.topLevelItemCount()))#.reverse()
    for i in aList[::-1]:
        widget.takeTopLevelItem(i)

It looks like it happens when I try to clear / do something in a succession.

Edit2:
Clicked debug with Visual Studios and got this message:
Unhandled exception at 0x000000006E9D2C68 (QtGui4.dll) in mayapy.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

I guess I am trying to access something that doesn’t exists? How is that possible?

Edit3:
Found out that it have something to do with my selection. But unfortunately it doesn’t help to deselect everything first.

[s]After some testing I appears that it is because I later try to get it’s invisibleroot item with self.treeWidget01.invisibleRootItem()

I use it because I am trying to add objects to it later when I populate it again. Does anyone know why it behaves this way?[/s]

Edit: Further testing say that it is because I clear not trying to get the invisibleroot.

ekhumoroat StackOverflowfound the problem, copy pasting his response here if someone stumbleupon the same problem:

This seems to be caused by a PySide bug in QTreeWidgetItemIterator when passing a QTreeWidgetItem as the first argument. When a QTreeWidget is passed as the first argument, the problem goes away. This bug does not occur with equivalent PyQt4 code.

The example code can be fixed by amending the following line in selectMirroredItems:

    # TreeWidgetItems = QtGui.QTreeWidgetItemIterator( topItems[0] )
    TreeWidgetItems = QtGui.QTreeWidgetItemIterator(widget)

It seems that simply iterating over the QTreeWidgetItemIterator is enough to cause the segfault (although I did not go to the trouble of attempting to create a minimal test case).

PySide bugs can be reported here.