Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete QTreeWidgetItem in PyQt?

I'm finding it frustratingly hard to find a simple way to delete my selected QTreeWidgetItem.

My patchwork method involves setting the tree's current selection to current and then:

if current.parent() is not None:
   current.parent().removeChild(current)
else:
   self.viewer.takeTopLevelItem(self.viewer.indexOfTopLevelItem(current))

It's not horrible, but isn't there a command that straight up just removes the item?

like image 203
RodericDay Avatar asked Aug 26 '12 21:08

RodericDay


2 Answers

PyQt4 uses sip to generate the python bindings for Qt classes, so you can delete the C++ object explicitly through the sip python API:

import sip
...
sip.delete(current)

The binding generator for PySide, shiboken, has a similar module.

like image 145
alexisdm Avatar answered Oct 28 '22 22:10

alexisdm


The QTreeWidget class has an invisibleRootItem() function which allows for a somewhat neater approach:

root = tree.invisibleRootItem()
for item in tree.selectedItems():
    (item.parent() or root).removeChild(item)
like image 26
ekhumoro Avatar answered Oct 29 '22 00:10

ekhumoro