---
title: "yield的应用——QTreeWidgetItem的迭代"
author: "icefire"
date: "2011-01-15T00:46:06Z"
tags: []
categories: ["编程语言"]
slug: "yield-qtreewidgetitem"
draft: false
lastmod: "2011-01-15T00:48:51Z"
id: 875076
url: "https://icefire.iteye.com/blog/875076"
---

*最近学python 用 pyside，所以做个笔记*

在Qt里，要是要遍历QTreeWidget所有节点，其中一个方法就是用QTreeWidgetItemIterator，但有个烦人的地方，这个东西在C++里还行，但在python里就太不python了。

```python

it = QTreeWidgetItemIterator(tree)
while it.value():
    v = it.value()
    do_something()
    it += 1

```

天呀，这么多行代码，还必须用while和it += 1。
传统简化方法，在js里，最典型的简化方法就是用模板。

```js

tree_item_loop = function(tree, fn) {
    it = QTreeWidgetItemIterator(tree)
    while (it.value()) {
        fn(it.value());
        it += 1
    }
}
tree_item_loop(tree, function(item){
    // 
})

```

在python里是这样

```python

def tree_item_loop(tree, fn):
    it = QTreeWidgetItemIterator(tree)
    while it.value():
        s_item = it.value()
        fn(s_item)
        it += 1

def fn(item):
    pass
tree_item_loop(tree, fn)

```

但是在又有一个问题，如果我需要知道当前节点在遍历时的index咋办？在循环里计算index也就算了，但更要命的是，python和js不一样，js里，fn这个参数，其本身的参数可以任意，甚至可以不声明，也就是说，定义成

```js

function some(item, index) {} 

```

或者 

```js

function(item) {} 

```

都是可以的，而python不行，必须要声明具体参数，即使是可变参数，也必须要声明，否则多传参数或少接收参数之类会报错。所以如果我要传index，所有传进来的fn就都必须是声明index或者 *args，这让调用者就郁闷了。

不过python既然这样无法解决，那么就用python的方法来解决。用**yield**。

```python

def tree_items(tree):
    it = QTreeWidgetItemIterator(tree)
    while it.value():
        yield it.value()
        it += 1

```


要遍历

```python

for item in tree_items(tree):
    print item

```

好爽。再来要带index

```python

for index, item in enumerate(tree_items(tree)):
    print index, item

```