Search code examples
pythonqtpyqtqtreewidgetqtreewidgetitem

Iteration of a QTreeWidget


I have a QTreeWidget that I am using to represent a breakdown structure of data. Basically I plan on using my QTreeWidget in a way to track the population of certain demographics in cities. For example:

 Males                                      9,000
     - New York                             5,000
     - D.C.                                 4,000

 Females                                    10,000
     - Nashville                            3,000
     - San Diego                            7,000

Obviously this data is just for example. A few problems I am having with implementing this. I can't figure out how to iterate over the tree. So that every so often I can check if the data has changed. If it has changed then I want to update it with the data I have stored in my python dictionary which looks something like:

{'Males': [MyCustomClass('New York', 5000), MyCustomClass('D.C.', 4000)], 
 'Females': [MyCustomClass('Nashville', 3000), MyCustomClass('San Diego', 7000)],
}

Any thoughts as to how I can iterate through each element (and thus each column of each element) and compare it against its stored dictionary value?


Solution

  • You can use the QTreeWidgetItemIterator

    Since I couldn't find the official pyqt documentation for it, something like this should work:

    iterator = QTreeWidgetItemIterator(self.treeWidget)
    
    while iterator.value():
        item = iterator.value()
        if item.text() == "someText" #check value here
            item.setText(column, "text") #set text here
        iterator+=1