Python Forum
[PyGUI] Expand last sub-node of treeview widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Expand last sub-node of treeview widget
#1
I have the following code:

import sys, os
from PySide2.QtWidgets import QApplication, QDialog, QTreeWidgetItem, QTreeWidget, QHBoxLayout, \
    QVBoxLayout, QStyleFactory


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setFixedHeight(400)
        self.layout = QHBoxLayout()
        self.layout.setSpacing(1)
        self.treeview = QTreeWidget()
        self.set_layout()
        self.set_treeview()
        self.setLayout(self.layout)


    def set_layout(self):

        vbox = QVBoxLayout()
        vbox.addWidget(self.treeview)

        self.layout.setSpacing(1)
        self.layout.addLayout(vbox)


        self.treeview.setFixedWidth(300)
        self.treeview.setFixedWidth(200)

    def set_treeview(self):
        self.treeview.clear()

        a = QTreeWidgetItem(self.treeview, ['A'])
        b = QTreeWidgetItem(self.treeview, ['B'])
        c = QTreeWidgetItem(self.treeview, ['C'])

        a_dict = {'a1':'test1', 'a2':'test2'}
        a_variables = ['p', 's']
        b_dict = {'b1':'test1'}
        b_variables = ['c', 'p']
        c_dict = {'c1':'test1', 'c2':'test2', 'c3':'test3'}
        c_variables = ['m', 'd']

        self.add_list_to_treeview(a, a_dict, a_variables)
        self.add_list_to_treeview(b, b_dict, b_variables)
        self.add_list_to_treeview(c, c_dict, c_variables)

    def add_list_to_treeview(self, parent, _dict, variables):
        for item in _dict:
            node = QTreeWidgetItem(parent, [item])
            if parent.text(0) == 'C':
                if _dict:
                    for variable in variables:
                        QTreeWidgetItem(node, variable)


if __name__ == '__main__':
    os.getcwd()
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    self = Form()
    self.show()
    sys.exit(app.exec_())
How can I expand only the last sub-node of 'C' in my treeview widget which is 'c3' in this case.
More sub-nodes exist, it should always expands the last node.

Help is much appreciated!
Reply
#2
Does nobody know how to do this? I really need some help so, any help is much appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,305 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  Treeview - SQL - Add child node snakes 0 2,416 May-15-2021, 08:48 PM
Last Post: snakes
  Treeview scroll selected node to top rfresh737 1 2,668 Apr-14-2021, 03:27 AM
Last Post: deanhystad
  [Tkinter] Treeview expand button size shamnadinn 8 7,572 Mar-08-2020, 12:02 PM
Last Post: shamnadinn

Forum Jump:

User Panel Messages

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