首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多级QTreeView

多级QTreeView
EN

Stack Overflow用户
提问于 2015-01-12 09:25:04
回答 1查看 6.5K关注 0票数 5

我很难理解如何使用QTreeView和QStandardItemModel设置多级QStandardItemModel。

我现在拥有的是:

代码语言:javascript
复制
from PySide.QtGui import *
import sys

class MainFrame(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        tree = {'root': {
                    "1": ["A", "B", "C"],
                    "2": {
                        "2-1": ["G", "H", "I"],
                        "2-2": ["J", "K", "L"]},
                    "3": ["D", "E", "F"]}
        }

        self.tree = QTreeView(self)
        root_model = QStandardItemModel()
        self.tree.setModel(root_model)

        for r,root in enumerate(sorted(tree)):
            root_item = QStandardItem(root)
            root_model.setItem(r,root_item)

            for c,child in enumerate(sorted(tree[root])):
                child_model = QStandardItemModel(root_model)
                child_item = QStandardItem(child)
                child_model.setItem(c, child_item)

                for gc, grand_child in enumerate(sorted(tree[root][child])):
                    grand_child_model = QStandardItemModel(child_model)
                    grand_child_item = QStandardItem(grand_child)
                    grand_child_model.setItem(gc,grand_child_item)

                    if type(tree[root][child]) == dict:
                        for ggc, gran_grand_child in enumerate(sorted(tree[root][child][grand_child])):
                            gran_grand_child_model = QStandardItemModel(grand_child_model)
                            gran_grand_child_item = QStandardItem(gran_grand_child)
                            gran_grand_child_model.setItem(ggc, gran_grand_child_item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MainFrame()
    main.show()
    sys.exit(app.exec_())

我想让它看起来像这样

到目前为止,只有“根”项显示,它没有抛出任何错误。

而且,我做这个for -循环有点匆忙,它很难阅读/理解,因为它有太多的嵌套循环。我想知道是否有更好的方法来扩展基于初始dict的树。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-12 17:35:27

您正在为每个项目创建一个模型。这是错误的。应该对项使用.appendRow/.insertRow来添加子项。

至于您的循环,我可能会使用递归方法来填充树。以下是我要做的:

代码语言:javascript
复制
from PySide.QtGui import *
import sys
import types

class MainFrame(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        tree = {'root': {
                    "1": ["A", "B", "C"],
                    "2": {
                        "2-1": ["G", "H", "I"],
                        "2-2": ["J", "K", "L"]},
                    "3": ["D", "E", "F"]}
        }

        self.tree = QTreeView(self)
        layout = QHBoxLayout(self)
        layout.addWidget(self.tree)

        root_model = QStandardItemModel()
        self.tree.setModel(root_model)
        self._populateTree(tree, root_model.invisibleRootItem())

    def _populateTree(self, children, parent):
        for child in sorted(children):
            child_item = QStandardItem(child)
            parent.appendRow(child_item)
            if isinstance(children, types.DictType):
                self._populateTree(children[child], child_item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MainFrame()
    main.show()
    sys.exit(app.exec_())

PS:主窗口也没有使用任何布局。我在上面加的。

PSv2:在python中最好避免类型检查,如果可能的话,但这在某种程度上是必要的,比如您构建tree的方式。如果您以不同的方式构造它,就像dict的所有方式一样,可能会更好:

代码语言:javascript
复制
tree = {'root': {
            '1': {'A':{}, 'B':{}, 'C':{}},
             ...
       }
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27898718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档