Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. CRUD for Python Objects

CRUD for Python Objects

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
5 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Hermans
    wrote on last edited by
    #1

    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
            self.email = 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):
            self.name = 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 pressed.....so 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

    JonBJ 1 Reply Last reply
    0
    • P Paul Hermans

      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
              self.email = 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):
              self.name = 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 pressed.....so 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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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.

      P 1 Reply Last reply
      2
      • JonBJ JonB

        @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.

        P Offline
        P Offline
        Paul Hermans
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Paul Hermans
          wrote on last edited by
          #4

          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.

          JonBJ 1 Reply Last reply
          0
          • P Paul Hermans

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Paul-Hermans
            I don't think there is any Python version. https://doc.qt.io/qtforpython/overviews/qtwidgets-itemviews-editabletreemodel-example.html 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 https://stackoverflow.com/questions/17278182/qtreeview-with-custom-items. No idea if it's good/relevant, but I see it manipulates index.internalPointer(). Might help you to look at that Python code?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved