In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the realization method of recursively traversing all current nodes with PyQt5 QTreeWidget tree structure". In daily operation, I believe that many people have doubts about the implementation method of recursively traversing all current nodes with PyQt5 QTreeWidget tree structure. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "what is the implementation method of recursively traversing all the current nodes in the PyQt5 QTreeWidget tree structure"! Next, please follow the editor to study!
The common methods in the QTreeWidget class describe setColumnWidth (int column Int width) sets the width of the specified column to the given value widthinsertTopLevelItems () inserts the item list in the top-level index of the view expandAll () expands all tree nodes invisibleRootItem () returns the root option not visible in the tree control selectedItems () returns a list of all selected non-hidden items the common method description in the QTreeWidgetItem class addChild () appends child items to the node text Text displayed by the setText () setting in the child list () returns the displayed node text setCheckState (column) State) sets the selected status of the specified column: Qt.Checked (node selected) Qt.Unchecked (Node not selected) setIcon (column,icon) displays an icon in the specified column to prepare a small demo
Common small demo
Introduction: category of goods and tree nested structure demo of commodities
Function: click the button to get all the currently selected items (with a little bit of contraband "▽")
Note: space is limited, the child node and the parent node are not selected, so when selecting the child node, please select the parent node yourself, otherwise it will be skipped.
Code block:
Import sysfrom PyQt5.QtWidgets import QTreeWidgetItem, QTreeWidget, QWidget, QVBoxLayout, QPushButton, QApplicationfrom PyQt5.QtCore import Qtclass Demo (QWidget): def _ init__ (self): super (). _ init__ () # instantiate a tree structure Hide header self.tree = QTreeWidget () self.tree.setHeaderHidden (True) # top branch self.tree_main = QTreeWidgetItem (self.tree) self.tree_main.setText (0, 'commodity category') # set some secondary branches tree_second = ['electronics', 'fruit', 'commodity' Self.gen_branch (self.tree_main, tree_second) # sets some tertiary branches tree_fruit = ['Apple', 'banana', 'pear'] tree_daily_use = ['paper towels', 'towels'] tree_lovers = ['Didi No.1' 'Didi No.2'] # child (1) means the first node of the branch. The serial number is calculated from 0 to self.gen_branch (self.tree_main.child (1), tree_fruit) self.gen_branch (self.tree_main.child (2), tree_daily_use) self.gen_branch (self.tree_main.child (3)) Tree_lovers) # A button self.pushButton = QPushButton ('selected') # shows self.qvl = QVBoxLayout () self.qvl.addWidget (self.tree) self.qvl.addWidget (self.pushButton) self.setLayout (self.qvl) # bind the slot function Pass in the main branch node self.pushButton.clicked.connect (lambda: self.get_checked (self.tree_main)) @ staticmethod def gen_branch (node: QTreeWidgetItem, texts: list): "given a node and list to branch within the node generation list" for text in texts: item = QTreeWidgetItem () item.setText (0) Text) item.setCheckState (0, Qt.Unchecked) node.addChild (item) def get_checked (self, node: QTreeWidgetItem)-> list: "" get all the branches selected by the current node Return a list "" temp_list = [] # see note 1 below here for item in node.takeChildren (): # determine whether to select if item.checkState (0) = = Qt.Checked: temp_list.append (item.text (0)) # to determine whether there are any child branches If item.childCount (): temp_list.extend (self.get_checked (item)) node.addChild (item) print (temp_list) return temp_listif _ _ name__ ='_ main__': app = QApplication (sys.argv) win = Demo () win.show () sys.exit (app.exec_ ())
Note 01: in this function, I pass in a node node, and the takeChildren () method takes out (deletes) all the first-level child branches of the node node and returns a list of all the first-level branches of the node, as shown below. This method can only return the first-level node information, using childCount () to determine whether there are child branches, there is recursion, all the way to the bottom node. Because takeChildren () deletes all nodes when it is fetched, it rejoins the node node after the operation ends.
[,] what are the advantages and disadvantages of this approach?
There is no doubt that the biggest advantage is that there is no need to create additional variables to store the information of the child nodes, and the information and order of the child nodes are obtained in real time rather than determined in advance. On the downside, I assume that there may be a change in the order of nodes if this method is used too much. For example, "apple, banana" has become "banana, apple", which has not appeared at present.
Compare the methods on the Internet
There is a way to do this about QTreeWidgetItemIterator, which is a built-in traversal in Qt, which is roughly as follows
Item = QtWidgets.QTreeWidgetItemIterator (self.treeWidget)
Use item.value () to locate a node, and the instance of item.value () is the kind of object in the list above, and you don't feel much different.
There is also a more violent practice. When generating child nodes, place all child nodes in the scope of the current class, that is, as attributes.
Self.item1 = QTreeWidgetItem ()
Or when it is generated, it is saved in a list defined in the scope, which has a downside: the information of the node is set in advance. But in fact, most of the situations encountered should be unknown.
Self.item_list = [] self.item_list.append ([...]) at this point, the study on "what is the implementation of recursively traversing all the current nodes in the PyQt5 QTreeWidget tree structure" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.