Iteration of a QTreeWidget

…衆ロ難τιáo~ 提交于 2021-02-11 06:20:18

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/31434725/iteration-of-a-qtreewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!