How does PyQt5 traverse all items under QTreeWidget? pyqt5qtreewidget

Source: Internet
Author: User

How does PyQt5 traverse all items under QTreeWidget? pyqt5qtreewidget

As a cainiao, you cannot describe the background of TAT as you did before describing the problem. By default, only those who read this article have already understood QTreeWidget, in python, we want to traverse all the items under it.


Because the software of the tester needs to be deleted after pressing the relevant function key.QTreeWidgetChecked. Therefore, you need to traverse all items to check the status. When reading the QT document (because the Reference Guide of PyQt5 has not been updated ...... so I had to point it to the C ++ Documentation link) and find that QT actually provides an iterator --QTreeWidgetItemIteratorIn its official documentation, it implements the following functions:

1     QTreeWidgetItemIterator it(treeWidget);2     while (*it) {3         if ((*it)->text(0) == itemText)4             (*it)->setSelected(true);5         ++it;6     }


For a cainiao, it is too difficult to change Python at a glance! -- So I went to Bing to see if there were any other methods. We found that this method is mostly used in PyQt:

1. UseQTreeWidgetOfTopLevelItemCount() ObtainTopLevelItemNumber

2. Use the for loop andTopLevelItem(IntIndex) Obtain the top-level node

3. ReuseQTreeWidgetItemTo obtain the children node under the top node.

Because I have been taught to pursue conciseness and watch QT clearly have ready-made functions but cannot use them, I cannot find a satisfactory answer in English search (Bing is used, but google cannot use it ), I will try againQTreeWidgetItemIteratorIf it takes a lot of time to complete the process without making any progress, you only need to use the code to test it. After N failures, an unrelated search result reminds you to use help in the Python interactive line to check whether it provides instructions here. In a try, there are:

class QTreeWidgetItemIterator(sip.simplewrapper) |  QTreeWidgetItemIterator(QTreeWidgetItemIterator) |  QTreeWidgetItemIterator(QTreeWidget, QTreeWidgetItemIterator.IteratorFlags flags=QTreeWidgetItemIterator.All) |  QTreeWidgetItemIterator(QTreeWidgetItem, QTreeWidgetItemIterator.IteratorFlags flags=QTreeWidgetItemIterator.All) |   |  Method resolution order: |      QTreeWidgetItemIterator |      sip.simplewrapper |      builtins.object |   |  Methods defined here: |   |  __iadd__(self, value, /) |      Return self+=value. |   |  __isub__(self, value, /) |      Return self-=value.

(The document is not complete. Here, only the small part required for this problem is shown)

All of a sudden, we see the dawn of the problem !! However, as a cainiao, although I saw Methods, I didn't immediately associate it with the pointer in C ++. I am still struggling withQTreeWidgetItemIteratorWhat should I return?isinstanceJudging whether it is neither an iterator nor an iterator )......

After hitting the wall several times, I suddenly reacted. The final implementation code is as follows:

Def delSelected (self): item = QtWidgets. QTreeWidgetItemIterator (self. treeWidget) # The value () of this class is QTreeWidgetItem while item. value (): if item. value (). checkState (0) = QtCore. qt. checked: # refer to the online method to determine whether there are any parent nodes and then perform the operations separately. The more direct self is not found. deleteNode () # Because I have deleted the node here, the location of the node changes after deletion, so we need to move back to item = item. _ isub _ (1) # To the next node item = item. _ iadd _ (1)

Is it very simple! Is this function available officially !! I don't need to consider the traversal efficiency of the cainiao is too easy to use !!

After the problem is solved, this article is also over, commemorating my crazy afternoon. Thank you for reading this! If any error occurs, please correct it!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.