Important: Please read the Qt Code of Conduct -

CRUD for Python Objects

  • I am brand new to pyQt and so far finding it incredibly difficult to understand how to create a basic application that does the following:

    1. Load a YAML file, storing data in python objects
    2. Display the data in a TreeView and allow the user to Add/Edit/Delete the data
    3. Write the Addes/Edited/Deleted data back out to the YAML file so it is available next time

    I made a few python objects to represent the data.

    Here are the python definitions of the classes that get used when reading the YAML file

    class Student:
        def __init__(self, last_name, first_name, email):
            self.last_name = last_name
            self.first_name = first_name
   = email
        def __str__(self):
            return self.last_name + ", " + self.first_name
        def __repr__(self):
            return self.__str__()
    class Course():
        def __init__(self, name, teacher_dir, student_dir, root_dir):
   = name
            self.teacher_dir = teacher_dir
            self.student_dir = student_dir
            self.root_dir = root_dir
            self.students = []

    The specific properties of a Course and a Student don't really matter, except that a Course contains a list of Students.

    How do I display this in a TreeView (or is there a better widget) and how do I make it so that a user can Add/Edit/Delete?

    Add - I envision opening a dialog to add a class....would have basic properties and a way to subsequently add/edit/delete students

    Edit - Same dialog as Add, just need to "connect" it to the original object and update that

    Delete - Assume Course is selected and then delete button need to figure out which Course object to delete.

    I tried reading about subclassing but couldn't find a clear explanation of which class to Subclass....and no real examples either.

    Can someone help me get the ball rolling?

    PS - I am using MacOsx

  • @Paul-Hermans
    Indeed the most appropriate view for this would be QTreeView. This requires a backing model. Your Courses will be the nodes and your Students will be the leaves.

    You should look at Qt examples: Simple Tree Model Example shows a simpler, non-editable example, while Editable Tree Model Example extends this to a fully editable model. That will provide the facilities you seek.

  • @JonB Thank you!
    I somehow missed that editable example. It looks like it should get me going so I appreciate your pointing it out!

  • My C++ is pretty rusty so finding it hard to follow and wondering if there is a python version of that example?

    Specifically, I think that both the TreeItem and TreeModel class are "custom" classes but the names mean nearly nothing to me. I get that they are used to store items in the tree etc, but I have no idea where the class that has actual properties that I want to use/display in the tree view is defined and what it is called. ....I am sure it is because I am brand new to Qt but finding this example very difficult to follow.

    Any help would be welcome.

  • @Paul-Hermans
    I don't think there is any Python version. is that example in the PySide2 documentation, and as you can see the code is still in C++! :) I think that is the case for a lot of Qt examples.

    Both TreeItem and TreeModel are indeed custom classes in that project. Indeed, if a class name does not start with Q... it's not a Qt-inbuilt! But the sample code reference page shows you all the code for them, and explains each bit.

    You might Google for e.g. python custom qtreeview code. I came across No idea if it's good/relevant, but I see it manipulates index.internalPointer(). Might help you to look at that Python code?

Log in to reply