PySide:QTreeView到嵌套字典 [英] PySide: QTreeView to nested dictionary

查看:131
本文介绍了PySide:QTreeView到嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来从QTreeView结构构建层次嵌套的字典,以获得类似这样的内容:

I need help to build a hierarchical nested dict from a QTreeView structure to get something like this:

 {"A": {"B": {"H": {}, "I": {"M": {}, "N": {}}}, "D": {}, "E": {}, "F": {}, "G": {"L": {}}, "C": {"J": {}, "K": {}}}}

{
    "A": {
        "B": {
            "H": {}, 
            "I": {
                "M": {}, 
                "N": {}
             }
        }, 
        "D": {}, 
        "E": {}, 
        "F": {}, 
        "G": {
            "L": {}
        }, 
        "C": {
            "J": {}, 
            "K": {}
        }
    }
}

在这种情况下,我不使用列,并且QTreeView表示一种目录结构(我实际上是从像上面的dict中提取出来的,只是想在修改Tree之后重新创建dict)

I am not using columns in this case and the QTreeView represents a directory structure (i actually extracted it from a dict like tho one above and just want to recreate the dict after modifying the Tree)

我已经有这样的东西了:

I already have some thing like this:

def to_dict(self, _structure={}, _parent=''):
    sublist[self.name()] = self._children

    for child in self._children:
        _structure[self.name()] = sublist
        child.to_dict(_structure, self.name())

很明显self._children是一个列表,因此无法正常工作

Obviously self._children is a list so it wont work

我想我可能需要这样的东西:

I think i might need something like this:

def to_dict(self, _structure={}, _parent=''):

    sublist = {self.name(): {}}

    for child in self._children:
        if _parent == '':
            _structure = sublist
        else:
            _structure[_parent].update(sublist)
        child.to_dict(_structure, self.name())

    return _structure

这里的问题是...我需要在_structure字典中找到_parent键,据我所知,它将始终位于dict的最低级别...我真的需要搜索整个_structure吗? dict averytime想要向给定的_parent添加新的下标,还是有更好的解决方案?

The Problem here is...i need to find the _parent key in the _structure dictionary and as far as i understand it will always be in the lowest level of the dict...do i really need to search the whole _structure dict averytime a want to add a new subdict to the given _parent or is there a better solution to my problem?

推荐答案

要将字典转换为模型,则必须对字典进行递归迭代,然后根据数据类型将其插入模型.在相反的情况下是相同的.

To convert the dictionary to a model then you have to iterate recursively on the dictionary, and according to the type of data insert it into the model. In the opposite case it is the same.

from PySide import QtCore, QtGui

def fill_model_from_json(parent, d):
    if isinstance(d, dict):
        for k, v in d.items():
            child = QtGui.QStandardItem(str(k)) 
            parent.appendRow(child)
            fill_model_from_json(child, v)
    elif isinstance(d, list):
        for v in d:
            fill_model_from_json(parent, v)
    else:
        parent.appendRow(QtGui.QStandardItem(str(d)))

def fill_dict_from_model(parent_index, d):
    v = {}
    for i in range(model.rowCount(parent_index)):
        ix = model.index(i, 0, parent_index)
        fill_dict_from_model(ix, v)
    d[parent_index.data()] = v

def model_to_dict(model):
    d = dict()
    for i in range(model.rowCount()):
        ix = model.index(i, 0)
        fill_dict_from_model(ix, d)    
    return d

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    tree = QtGui.QTreeView()
    model = QtGui.QStandardItemModel()
    data =  {"A": {"B": {"H": {}, "I": {"M": {}, "N": {}}}, "D": {}, "E": {}, "F": {}, "G": {"L": {}}, "C": {"J": {}, "K": {}}}}
    fill_model_from_json(model.invisibleRootItem(), data)
    tree.setModel(model)
    tree.expandAll()
    tree.resize(360, 480)
    tree.show()
    d = model_to_dict(model)
    assert(d == data)
    print(d)
    sys.exit(app.exec_())

这篇关于PySide:QTreeView到嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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