PyQt5基础学习-QTreeWidget().selectedItem().parent().removeChild(删除当前所选的节点)
作者:互联网
主要介绍节点的删除, 节点的添加, 节点的修改
ModifyTree.py
""" 添加, 修改和删除树控件中的节点 """ import sys from PyQt5.QtWidgets import * class ModifyTree(QWidget): def __init__(self): super(ModifyTree, self).__init__() self.setWindowTitle("TreeWidget 例子") operatorLayout = QHBoxLayout() addBtn = QPushButton("添加节点") updateBtn = QPushButton("修改节点") deleteBtn = QPushButton("删除节点") operatorLayout.addWidget(addBtn) operatorLayout.addWidget(updateBtn) operatorLayout.addWidget(deleteBtn) addBtn.clicked.connect(self.addNode) updateBtn.clicked.connect(self.updateNode) deleteBtn.clicked.connect(self.deleteNode) self.tree = QTreeWidget() self.tree.setColumnCount(2) self.tree.setHeaderLabels(["Key", "Value"]) root = QTreeWidgetItem(self.tree) root.setText(0, 'root') root.setText(1, '0') child1 = QTreeWidgetItem(root) child1.setText(0, 'child1') child1.setText(1, '1') child2 = QTreeWidgetItem(root) child2.setText(0, 'child2') child2.setText(1, '2') child3 = QTreeWidgetItem(child2) child3.setText(0, 'child3') child3.setText(1, '3') self.tree.clicked.connect(self.onTreeClicked) mainLayout = QVBoxLayout(self) mainLayout.addLayout(operatorLayout) mainLayout.addWidget(self.tree) self.setLayout(mainLayout) def onTreeClicked(self, index): item = self.tree.currentItem() print(index.row()) print("key=%s, value=%s"%(item.text(0), item.text(1))) #添加节点 def addNode(self): print("添加节点") #获得当前的节点 item = self.tree.currentItem() print(item) #在当前节点上新建节点 node = QTreeWidgetItem(item) node.setText(0, "新节点") node.setText(1, "新值") def updateNode(self): print("修改节点") #获得当前的节点 item = self.tree.currentItem() #进行节点的修改 item.setText(0, "修改节点") item.setText(1, "值已经被修改") def deleteNode(self): print("删除节点") #获得根节点的父节点 root = self.tree.invisibleRootItem() #获得所选的节点 for item in self.tree.selectedItems(): #获得其父节点然后再删除其子节点 (item.parent() or root).removeChild(item) if __name__ == "__main__": app = QApplication(sys.argv) main = ModifyTree() main.show() sys.exit(app.exec_())
标签:setText,parent,selectedItem,tree,PyQt5,self,item,root,节点 来源: https://www.cnblogs.com/hyq-lst/p/15864302.html