The tree of pyqt5 (example tutorial)

Description:
   python 3.5 ,pyqt5..


import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class TreeWidget(QMainWindow):
    myControls ={}
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.setWindowTitle('TreeWidget')
        self.tree = QTreeWidget()
        self.myControls['tree']=self.tree
        self.tree.setColumnCount(2) # Indicates that it is a tree-shaped table,
        self.tree.setHeaderLabels(['Key','Value']) # If it is a table, there is a header
        # The parent of the root node is the QTreeWidget object
        root= QTreeWidgetItem(self.tree)

        root.setText(0,'root')
        child1 = QTreeWidgetItem(root) #Point out the parent node
        child1.setText(0,'child1')
        child1.setText(1,'name1')
        child2 = QTreeWidgetItem(root)
        child2.setText(0,'child2')
        child2.setText(1,'name2')
        child3 = QTreeWidgetItem(root)
        child3.setText(0,'child3')
        child4 = QTreeWidgetItem(child3)
        child4.setText(0,'child4')
        child4.setText(1,'name4')
        #The following two sentences are the settings of the main window
        self.tree.addTopLevelItem(root)
        self.setCentralWidget(self.tree)

        #With icon is this form QAction(QIcon("ss.png"), "add", self)
        addAction = QAction( "Add", self)
        addAction.triggered.connect(self.on_addAction_triggered)

        editAction = QAction("modify", self)
        editAction.triggered.connect(self.on_editdAction_triggered)

        deleteAction = QAction("delete", self)
        deleteAction.triggered.connect(self.on_deleteAction_triggered)

        findAction = QAction("find", self)
        findAction.triggered.connect(self.on_findAction_triggered)

        toolbar = self.addToolBar("aa")
        toolbar.addAction(addAction)
        toolbar.addAction (editAction)
        toolbar.addAction(deleteAction)
        toolbar.addAction(findAction)

    def on_addAction_triggered(self):
        currNode = self.tree.currentItem()
        addChild1 =QTreeWidgetItem()
        addChild1.setText(0,'addChild1_key')
        addChild1.setText(1, 'addChild1_val')
        currNode.addChild(addChild1)

    def on_editdAction_triggered(self):
        currNode = self.tree.currentItem()
        currNode.setText(0,'editkey')
        currNode.setText(1, 'editvalue')

    def on_deleteAction_triggered(self):
        currNode = self.tree.currentItem()
        parent1=currNode.parent();
        parent1.removeChild (currNode)

    def on_findAction_triggered(self):
         #MatchRegExp Regular search, MatchRecursive recursive traversal, and finally refers to the column value of the tree table
         #This example is to find all nodes in the 0th with the word "child" at the beginning
        nodes=self.tree.findItems ("^child[\w|\W]*",Qt.MatchRegExp | Qt.MatchRecursive  ,0)
        for node in nodes :
            QMessageBox.information(self, '', node.text(0))


app = QApplication(sys.argv)
tp = TreeWidget()
tp.show()
app.exec_()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326506220&siteId=291194637