0

I want to build a QTreeWidget with a JSON file that contains several family generational levels, using recursion. Here is my code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from PyQt5 import QtCore
import json
#read in JSON family structure
def readJsonData(fileName):
    with open(fileName) as json_data:
        treeData = json.load( json_data )
    return treeData   

fileName = 'family.json'     

d = readJsonData(fileName)

myList = []
def myprint( d, myList ):
  for k, v in d.items():
    if isinstance(v, dict):
      myprint(v, myList )
      myList.append(k)
    else:
      myList.append(k)
  return myList

a = myprint(d, myList )

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.tree = QTreeWidget(self)

        self.tree.setSelectionMode(QAbstractItemView.SingleSelection)

        editKey = QShortcut(QKeySequence(Qt.Key_Return), self.tree)
        self.setCentralWidget(self.tree)
        self.tree.setHeaderLabel('Tree')
        i = QTreeWidgetItem(self.tree, [a[len(a)-1]])
        a.pop()
        self.tree.addTopLevelItem(i)
        for x in a:
            QTreeWidgetItem(i, ['{}'.format(x)])

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

This creates a family tree with root mainMenu and members as:

['grandChild12',
 'grandChild11',
 'Child1',
 'grandChild21',
 'grandChild22',
 'Child2',
 'grandChild31',
 'grandChild32',
 'Child3']

But, the result should be nested like the JSON file structure:

'mainMenu':
   'Child1'
       'grandChild11'
       'grandChild12'
   'Child2'
       'grandChild21'
       'grandChild22'
   'Child3'
       'grandChild31'
       'grandChild32'

How do I adapt the line:

for x in a:
    QTreeWidgetItem(i, ['{}'.format(x)])

to create the correct structure with recursion? I've done this before in Java, so I know it can be done by walking up a limb to a node and down to a terminal leaf, either from left or center or right of the main stem.

The JSON file is presented below:

{
  "mainMenu": {
    "Child1": {
      "grandChild11": "None",
      "grandChild12": "None"
    },
    "Child2": {
      "grandChild21": "None",
      "grandChild22": "None"
    },
    "Child3": {
      "grandChild31": "xx",
      "grandChild32": "xy"
    }
  }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Shows your .JSON file – eyllanesc Jul 24 '18 at 21:09
  • Should I structure the content of the JSON file to contain objects() to be populated later? See: https://stackoverflow.com/questions/21805047/qtreewidget-to-mirror-python-dictionary – oldHornPlayer Jul 25 '18 at 14:57
  • Possible duplicate of [QTreeWidget to Mirror python Dictionary](https://stackoverflow.com/questions/21805047/qtreewidget-to-mirror-python-dictionary) – eyllanesc Jul 25 '18 at 14:59
  • yes, and your question is a duplicate of that question – eyllanesc Jul 25 '18 at 14:59
  • Yes, I read that post. I'm working it now. My version of JSON Editor (Microsoft) does not allow me to create the key-object nested dictionary structure, so I'm working it manually. Sigh. – oldHornPlayer Jul 25 '18 at 17:26

0 Answers0