QTreeWidget 的迭代 [英] Iteration of a QTreeWidget

查看:144
本文介绍了QTreeWidget 的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTreeWidget,用于表示数据的分解结构.基本上我计划使用我的 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

显然这个数据只是举例.我在实现这一点时遇到了一些问题.我不知道如何遍历树.这样我就可以经常检查数据是否已更改.如果它已经改变,那么我想用我存储在我的 python 字典中的数据更新它,它看起来像:

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?

推荐答案

你可以使用 QTreeWidgetItemIterator

You can use the QTreeWidgetItemIterator

由于我找不到它的官方 pyqt 文档,因此应该可以使用以下内容:

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

这篇关于QTreeWidget 的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆