PyQt5 How to implement the traversal of all item under Qtreewidget

Source: Internet
Author: User

Preface: As a rookie, can not like your great God in the description of the problem before the introduction of the background tat, only the default view of this article of the people to Qtreewidget has already had the understanding, wants in Python to implement all the item in the traversal.

Since the hackers software needs to implement pressing the relevant function keys, delete the qtreewidget tick (that is, the checked) item. So all item check states need to be traversed. While reading the QT documentation (because PYQT5 's Reference guide hasn't been updated yet ...). So had to point it to the C + + documentation link), found that QT actually provides an iterator--qtreewidgetitemiterator, in its official document, it is so implemented:

1     Qtreewidgetitemiterator It (treewidget); 2      while (*it) {3         if ((*it)->text (0) = = Itemtext) 4             (*it)->setselected (true); 5         + +it; 6     }

......

For a rookie, according to this one to see how to change in Python is too difficult ah! -So I went to Bing first and there was no other way. It is found that most people are using this method under PYQT:

1, first use qtreewidget toplevelitemcount() to obtain the number of Toplevelitem

2. Then use the For loop and toplevelitem(int index) to get the top-level nodes.

3, and then use the function of the Qtreewidgetitem to obtain the children node under the top layer node.

Because has been taught to pursue simplicity, and look at QT clearly have ready-made features can not be used, so in the English search also did not find satisfactory answer (with or Bing, Google can not use), I again back to ponder qtreewidgetitemiterator, In the case of spending a lot of time still without progress, as long as the first directly with the code test, failed n times, was an unrelated search result reminder can be in the Python interactive line with help to see if it is here to provide documentation, a try, sure enough:

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 finished, here only the small part of the problem required)

Suddenly saw the problem solved the dawn! However, as a rookie, although I saw methods, but did not immediately connect it with C + + pointers, still in the tangled pyqt qtreewidgetitemiterator return should be what (with isinstance Judge that it is neither an iterator nor an iterative) ...

So after a few more hits on the wall, I suddenly reacted, the final implementation code is as follows:

defdelselected (self): item=qtwidgets.qtreewidgetitemiterator (self.treewidget)#the value () of the class is Qtreewidgetitem     whileitem.value ():ifItem.value (). CheckState (0) = =QtCore.Qt.Checked:#refer to the method on the Internet, to determine whether there is no parent node after the operation of the one, really can not find a more directSelf.deletenode ()#because I am here to delete the end point, delete the location of the node changes, so to step backItem = Item.__isub__(1)#to the next nodeItem = Item.__iadd__(1)

Very simple there is no! The official provision of this function is useful there is no!! For me this does not need to consider the efficiency of the novice is too good to have!!

The problem is solved, and this is the end of the article, to commemorate my crazy afternoon. Thank you for reading here! Please correct me if there is any mistake!

  

PyQt5 How to implement the traversal of all item under Qtreewidget

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: info-contact@alibabacloud.com 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.