[PyQt] Can't delete a wanted item in a treeView

Maurizio Berti maurizio.berti at gmail.com
Fri May 31 15:13:58 BST 2019


Il giorno ven 31 mag 2019 alle ore 09:51 Gottfried Müller <
gottfried.mueller at gmx.de> ha scritto:

> Hello,
>
> in the example I created a tree with 3 items ("aaa", "bbb", "ccc"). In a
> context menu I want to remove the items "bbb" or "ccc". I get always the
> correct item, but I have no idea how to delete this item. May action
> takes always the item "aaa". In another menu action ("find item text") I
> searched for a reason. I get always the row=0 and column=0 of the model
> index. Also a findItems call delivers no results. I have no idea whats
> wrong.
>

You're trying to remove an item which is child of another one, while
QStandardItemModel.takeItem() only works for "root" items.
You need to remove the item's row from the item parent:

        [...]
        delCase01.triggered.connect(lambda: self.delTakeItem(item))
        menu.exec_(self.mapToGlobal(pos))

     def delTakeItem(self, item):
         item.parent().removeRow(item.row())

Cheers,
Maurizio

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190531/4ce84ab2/attachment.html>


More information about the PyQt mailing list