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

Gottfried Müller gottfried.mueller at gmx.de
Fri May 31 08:50:15 BST 2019


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.

Gottfried

Example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring

import sys

from PyQt5.QtWidgets import QApplication, QTreeView, QMenu
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt


class MainWindow(QTreeView):

     def __init__(self, parent=None):
         super().__init__(parent)
         self.mdl = QStandardItemModel(parent=self)
         self.setModel(self.mdl)
         self.fillModel()
         self.setContextMenuPolicy(Qt.CustomContextMenu)
         self.customContextMenuRequested.connect(self.ctxMenu)

     def fillModel(self):
         self.mdl.clear()
         item01 = QStandardItem("aaa")
         item02 = QStandardItem("bbb")
         item03 = QStandardItem("ccc")
         item02.appendRow(item03)
         item01.appendRow(item02)
         self.mdl.appendRow(item01)
         self.expandToDepth(1)

     def ctxMenu(self, pos):
         mdlIdx = self.indexAt(pos)
         if not mdlIdx.isValid():
             return
         item = self.mdl.itemFromIndex(mdlIdx)
         if item.text() == "aaa":
             return
         menu = QMenu(self)
         findItem = menu.addAction("Find item text")
         findItem.triggered.connect(lambda: self.findItemText(mdlIdx, item))
         delCase01 = menu.addAction("Delete takeItem case")
         delCase01.triggered.connect(lambda: self.delTakeItem(mdlIdx, item))
         menu.exec_(self.mapToGlobal(pos))

     def findItemText(self, mdlIdx, itemMenu):
         mdlIdxF = self.mdl.indexFromItem(itemMenu)
         itemF = self.mdl.itemFromIndex(mdlIdxF)
         itemMdlIdx = self.mdl.itemFromIndex(mdlIdx)
         print("find01:", itemMenu.text(), itemF.text(), itemMdlIdx.text())
         for fItem in self.mdl.findItems(itemMenu.text()):
             print('find02', fItem.text())
         print("findRowColMenu:", mdlIdx.row(), mdlIdx.column())
         print("findRowColMdlF:", mdlIdxF.row(), mdlIdxF.column())
         print("selectedIndexes:",
               [(idx.row(), idx.column()) for idx in self.selectedIndexes()]
         )

     def delTakeItem(self, mdlIdx, itemMenu):
         delItemMdlIdx = self.mdl.indexFromItem(itemMenu)
         itemDel = self.mdl.itemFromIndex(delItemMdlIdx)
         itemMdlIdx = self.mdl.itemFromIndex(mdlIdx)
         print("tk01", itemMenu.text(), itemDel.text(), itemMdlIdx.text())
         takeItem = self.mdl.takeItem(mdlIdx.row())
         #takeItem = self.mdl.takeItem(delItemMdlIdx.row())
         #takeItem = self.mdl.takeItem(itemMdlIdx.row())
         print("tk02", takeItem.text())


def main():
     app = QApplication(sys.argv)
     mainWindow = MainWindow()
     mainWindow.show()
     return app.exec_()


if __name__ == "__main__":
     main()



More information about the PyQt mailing list