Python Forum
[PyQt] How to populate a treeview on a GUI with a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to populate a treeview on a GUI with a dictionary
#1
I have a treeview on my GUI called 'treeView'.
I have a dictionary which contains the following:

{"Pipe 1": {'1.Name': 'ABC', '2.Outside diameter': '10', '3.Wall thickness': '5', '4.Density': '7850', '5.Liner layers': {1: {'1.Liner name': 'SAD', '2.Liner thickness': '5', '3.Liner density': '900'}}, '6.Coating layers': {1: {'1.Coating name': 'TWR', '2.Coating thickness': '50', '3.Coating density': '3000', '4.Coating cutback': '0.7', '5.Coating absorption': '4'}}}}
How do I get the dictionary into the respective treeview such that it shows:

Pipe 1
1.Name
2.Outer diameter
3.Wall thickness
4.Density
5.Liner layers
1. Liner name
2. Liner thickness
3. Liner density
6.Coating layers
1.Coating name
2.Coating thickness
3.Coating density
4.Coating cutback
5.Coating absorption

I have found the below code but, it shows a new window instead of showing it into the treeview on the GUI and I have no experience in adjusting it to suit my needs yet.

from PyQt5.QtWidgets import  QApplication, QTreeWidget, QTreeWidgetItem

class ViewTree(QTreeWidget):
    def __init__(self, value):
        super().__init__()
        def fill_item(item, value):
            def new_item(parent, text, val=None):
                child = QTreeWidgetItem([text])
                fill_item(child, val)
                parent.addChild(child)
                child.setExpanded(True)
            if value is None: return
            elif isinstance(value, dict):
                for key, val in sorted(value.items()):
                    new_item(item, str(key), val)
            elif isinstance(value, (list, tuple)):
                for val in value:
                    text = (str(val) if not isinstance(val, (dict, list, tuple))
                            else '[%s]' % type(val).__name__)
                    new_item(item, text, val)
            else:
                new_item(item, str(value))

        fill_item(self.invisibleRootItem(), value)

if __name__ == '__main__':
    app = QApplication([])
    window = ViewTree({"Pipe 1": {'1.Name': 'ABC', '2.Outside diameter': '10', '3.Wall thickness': '5', '4.Density': '7850', '5.Liner layers': {1: {'1.Liner name': 'SAD', '2.Liner thickness': '5', '3.Liner density': '900'}}, '6.Coating layers': {1: {'1.Coating name': 'TWR', '2.Coating thickness': '50', '3.Coating density': '3000', '4.Coating cutback': '0.7', '5.Coating absorption': '4'}}}})
    window.show()
    app.exec_()
Additionally it shows 1 on the top, instead of starting at 'Pipe 1'.
Reply
#2
Here is a tree view that I wrote that displays Category with their Sub-Groups a version of this should work for you and it works within the tree view itself
    def SetContent(self, tmpCatGrpList):
        self.model.setRowCount(0)
        TreeRoot = self.model.invisibleRootItem()
        SeenCat = {}
        QuedData = deque(tmpCatGrpList)

        while QuedData:
            Itm = QuedData.popleft()
            CurNode = TreeRoot
            CatNam = Itm['CatgryName']
            GrpNam = Itm['GroupName']

            if CatNam in SeenCat:
                CurNode = SeenCat[CatNam]
                CurNode.appendRow([QStandardItem(GrpNam)])
            else:
                CurNode.appendRow([QStandardItem(CatNam)])
                if len(GrpNam) > 0:
                    CurNode = CurNode.child(CurNode.rowCount() - 1)
                    CurNode.appendRow([QStandardItem(GrpNam)])

                SeenCat[CatNam] = CurNode
        
        # Initialize Selection to First Row 'All'
        self.setCurrentIndex(self.model.index(0, 0))
Note you most likely will not need the QuedData = deque(tmpCatGrpList) line as yours seems to be a straight pass through but who knows
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] populate a QListWidget devilonline 1 1,524 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,055 Jul-31-2022, 09:01 PM
Last Post: Extra
  Problems getting tk Combobox contents to populate properly dford 4 3,706 Jan-08-2022, 02:39 PM
Last Post: dford
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,301 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  Auto populate dictionary with names/values of QT widgets cjh 1 2,895 Mar-23-2021, 02:56 PM
Last Post: deanhystad
  populate list with images and be able to select them ricardons 0 2,103 Jan-11-2019, 03:45 PM
Last Post: ricardons
  Hardest time getting DataFrame to populate Tree Widget WuchaDoin 4 7,293 Oct-15-2018, 08:29 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020