0

I am migrating a project from python27 to python37 so that I could switch from using PyQt to PySide. When refactoring my code, I am now unable to delete instances of my custom class that inherits from the QTreeWidgetItem

class CustomTreeItem(qtwidget.QTreeWidgetItem):

Originally, I was just doing sip.delete(CustomTreeItem object). However, after the migration I tried doing the same thing with shiboken2.delete(CustomTreeItem object) and received the error

Process finished with exit code -1073741819 (0xC0000005)

Thinking that the error was related to shiboken2, I tried other ways of deleting

assm_tree_widget.clear()

and

selections = assm_tree_widget.selectedItems()
for item in selections:
    index = assm_tree_widget.indexOfTopLevelItem(item)
    assm_tree_widget.takeTopLevelItem(index)

Both of these throw similar exit codes.

Remus Wong
  • 27
  • 4
  • 1
    It's normally not required to call `sip.delete`, why are you using it? – musicamante Jun 18 '21 at 16:52
  • I didn't write the py27 code, just migrating. What would be the proper way to delete an instance of a QTreeWidgetItem? `shiboken2.delete` works fine on normal QTreeWidgetItem objects but breaks on my custom class. I tried the other alternatives I listed just to see if I could even delete the CustomTreeItem but those didn't even seem to work (not sure if they utilize shiboken under the hood). – Remus Wong Jun 18 '21 at 17:29
  • If you're going to clear the whole tree widget with `clear()`, you don't need to delete anything, as Qt will take care of it (and python's garbage collector too). – musicamante Jun 18 '21 at 17:31
  • I was just using `clear()` as an experiment to see if I could delete something in the tree without a direct shiboken call. It is not the intended functionality I want. However, it yields similar errors. Perhaps there is something wrong with my CustomTreeItem after the py37 migration? Its behavior seems correct to me which is why I'm asking about the inherited class. – Remus Wong Jun 18 '21 at 17:43
  • I was specifically looking at this method for deleting TreeWidgetItems here. https://stackoverflow.com/a/52675468/16262261 – Remus Wong Jun 18 '21 at 17:53
  • @RemusWong please provide a [mre] – eyllanesc Jun 18 '21 at 20:05
  • If you want to remove an item from a QTreeWidget, use `takeTopLevelItem()` if it's a top level, otherwise `removeChild(item)` on the parent (including the `invisibleRootItem()`, which allows removing top level items too). If there's no other reference on the item, then it will be automatically deleted. It could be useful to know if there's a specific reason for trying to use `del`, and a MRE would help as already requested. – musicamante Jun 18 '21 at 22:04
  • Ok I seem to have found he problem. There isn't anything inherently wrong with deleting a custom defined class but problems will occur if you mix `shiboken2.delete` calls with methods from the QTreeWidget that remove items that aren't native to PySide2. This is understandable behavior as for why I was getting memory access related errors. So, the solution was to just replace all my `shiboken.delete()` with standard `takeTopLevelItem()` methods. – Remus Wong Jun 21 '21 at 14:14

0 Answers0