PySide: QTreeView to nested dictionary

2020-07-30 09:58发布

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": {}
        }
    }
}

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())

Obviously self._children is a list so it wont work

EDIT: 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

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?

1条回答
爷、活的狠高调
2楼-- · 2020-07-30 10:43

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_())

enter image description here

查看更多
登录 后发表回答