PyQt5: How to generate a QTreeView from a list of dictionary items?

tunchunairarko picture tunchunairarko · Nov 3, 2017 · Viewed 7.9k times · Source

I have a dataset like this:

[{'level': 0, 'dbID': 77, 'parent_ID': 6, 'short_name': '0:0:0:<new> to 6', 'long_name': '', 'order': 1, 'pos': 0} ,
{'level': 1, 'dbID': 88, 'parent_ID': 77, 'short_name': '1:1:1:Store13', 'long_name': '', 'order': 2, 'pos': 1} ,
{'level': 0, 'dbID': 442, 'parent_ID': 6, 'short_name': '2:<new>', 'long_name': '', 'order': 1, 'pos': 2} ,
{'level': 1, 'dbID': 522, 'parent_ID': 442, 'short_name': '3:<new>', 'long_name': '', 'order': 2, 'pos': 3} ,
{'level': 2, 'dbID': 171, 'parent_ID': 522, 'short_name': '3:<new>', 'long_name': '', 'order': 1, 'pos': 3} ,
{'level': 0, 'dbID': 456, 'parent_ID': 6, 'short_name': '4:<new>', 'long_name': '', 'order': 1, 'pos': 4} ,
{'level': 1, 'dbID': 523, 'parent_ID': 456, 'short_name': '5:<new>', 'long_name': '', 'order': 3, 'pos': 5}]

Here "level" means at which level of the tree the data will be. Level 0 means parent node, 1 means child of 0, 2 means child of 1 and so on. I have to generate a QTreeView from it. I have been trying in many different ways to do it, but I'm failing. Here's the code which I've written so far:

THE FUNCTION:

def populateTree(self):
    tree=self.structureData
    root_model=MoDaTreeModel()
    self.treeViewStructure.setModel(root_model)
    self.recursiveAdd(tree,0,root_model)

def recursiveAdd(self,tree,n,parent):
    for i in range(n,len(tree)):            
        if(tree[i]['level']<=tree[i+1]['level']):
            child_item=QStandardItem()
            child_item.setText(str(tree[i]['short_name']))
            parent.appendRow(child_item)
        else:
            self.recursiveAdd(tree,i+1) #I want to write the child node, but don't know how

Can someone help me as to how I can proceed from here?

Answer

ekhumoro picture ekhumoro · Nov 3, 2017

Since your data structure isn't hierarchical, an iterative solution is more appropriate. With a flat data structure, the parent of each item isn't always available in advance, so some items must be pushed back onto a stack until their parents have been processed.

Here is a simple demo based on the data in your question:

screenshot

import sys
from collections import deque
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self, data):
        super(Window, self).__init__()
        self.tree = QtWidgets.QTreeView(self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tree)
        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(['Name', 'dbID'])
        self.tree.header().setDefaultSectionSize(180)
        self.tree.setModel(self.model)
        self.importData(data)
        self.tree.expandAll()

    def importData(self, data, root=None):
        self.model.setRowCount(0)
        if root is None:
            root = self.model.invisibleRootItem()
        seen = {}
        values = deque(data)
        while values:
            value = values.popleft()
            if value['level'] == 0:
                parent = root
            else:
                pid = value['parent_ID']
                if pid not in seen:
                    values.append(value)
                    continue
                parent = seen[pid]
            dbid = value['dbID']
            parent.appendRow([
                QtGui.QStandardItem(value['short_name']),
                QtGui.QStandardItem(str(dbid)),
                ])
            seen[dbid] = parent.child(parent.rowCount() - 1)

if __name__ == '__main__':

    data = [
        {'level': 0, 'dbID': 77, 'parent_ID': 6, 'short_name': '0:0:0:<new> to 6', 'long_name': '', 'order': 1, 'pos': 0} ,
        {'level': 1, 'dbID': 88, 'parent_ID': 77, 'short_name': '1:1:1:Store13', 'long_name': '', 'order': 2, 'pos': 1} ,
        {'level': 0, 'dbID': 442, 'parent_ID': 6, 'short_name': '2:<new>', 'long_name': '', 'order': 1, 'pos': 2} ,
        {'level': 1, 'dbID': 522, 'parent_ID': 442, 'short_name': '3:<new>', 'long_name': '', 'order': 2, 'pos': 3} ,
        {'level': 2, 'dbID': 171, 'parent_ID': 522, 'short_name': '3:<new>', 'long_name': '', 'order': 1, 'pos': 3} ,
        {'level': 0, 'dbID': 456, 'parent_ID': 6, 'short_name': '4:<new>', 'long_name': '', 'order': 1, 'pos': 4} ,
        {'level': 1, 'dbID': 523, 'parent_ID': 456, 'short_name': '5:<new>', 'long_name': '', 'order': 3, 'pos': 5}
        ]

    app = QtWidgets.QApplication(sys.argv)
    window = Window(data)
    window.setGeometry(600, 50, 400, 250)
    window.show()
    sys.exit(app.exec_())