Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyside2 - How to create notes/comments and display them corresponding to selected item from list



  • I have been creating a tool that displays multiple file versions from a folder in a QListWidget.

    I would like to save notes/comments for each version and display them using QTextEdit corresponding to the item in the list that i click. Like a version manger. How shall i go about this? Kinda stuck... Thank you!

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    
    
    class Dialog(QtWidgets.QDialog):
        NumList = 20
    
        def __init__(self):
            super(Dialog, self).__init__()
    
            self.createHorizontalGroupBox()
            self.createVerticalGroupBox()
            mainLayout = QtWidgets.QVBoxLayout()
            mainLayout.addWidget(self.verticalGroupBox)
            mainLayout.addWidget(self.horizontalGroupBox)
            self.setWindowTitle("File Manager v1.0")
            self.setLayout(mainLayout)
    
    
    
        def createVerticalGroupBox(self):
            self.verticalGroupBox = QtWidgets.QGroupBox("List Files")
            layout = QtWidgets.QVBoxLayout()
            layout.setContentsMargins(25,25,25,25)
            layout.setSpacing(7)
    
            file_listbox = QtWidgets.QListWidget()
            for i in range(Dialog.NumList):
                file_list = ("File_v%d" % (i+1))
                file_listbox.addItem(file_list)
            layout.addWidget(file_listbox)
    
            self.verticalGroupBox.setLayout(layout)
    
    
        def createHorizontalGroupBox(self):
            self.horizontalGroupBox = QtWidgets.QGroupBox("Notes")
            layoutB = QtWidgets.QGridLayout()
            layoutB.setContentsMargins(25, 25, 25, 25)
            layoutB.setSpacing(7)
            layoutB.setColumnStretch(0,5)
    
            self.notes_editor = QtWidgets.QTextEdit()
            self.notes_editor.setPlainText("Test..")
            layoutB.addWidget(self.notes_editor,0, 2, 4, 1)
    
            self.edit_button = QtWidgets.QPushButton("Edit..")
            layoutB.addWidget(self.edit_button,0,5)
    
            self.save_button = QtWidgets.QPushButton("Save")
            layoutB.addWidget(self.save_button,1,5)
    
            self.horizontalGroupBox.setLayout(layoutB)
    
    
    
    
    
    app = QtWidgets.QApplication(sys.argv)
    dialog = Dialog()
    sys.exit(dialog.exec_())
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you storing these different file versions ?



  • @SGaist Cheers!
    If i understood you right, i'm storing these versions as fileName_v001, fileName_v002 and so on...


  • Lifetime Qt Champion

    Then this might come as a silly question but since there's VCS like git, are you sure you are using the tool ?

    Anyway, for your current system, you could have a second file following the name schema with a different extension for the notes. Or have them in a SQLite database.



  • @SGaist That's where i'm stuck, i have no idea how to implement it with PySide. I like to manually type my tools using examples/forum guides as reference. But examples for vcs with Qt is something i never came across with google search. Which is surprising...


  • Lifetime Qt Champion

    @younglegend said in Pyside2 - How to create notes/comments and display them corresponding to selected item from list:

    But examples for vcs with Qt is something i never came across with google search

    Well, it does not have anything to do with Qt actually. Qt is a C++ framework, so you write code using Qt as framework. In your case it's Python code, but this does not change anything. Just use a VCS as you could do with any project.



  • @younglegend
    I'm not suggesting it's a good idea to do it this way, but....

    If you do not want to go down the road of a VCS tool or a database or similar to hold your file versions and comments (which is a whole different ballgame), but instead stick to no more than the simple file handling you currently have, then:

    Your files seem to be named File_vnumber. So store your comments in similarly named files, so you can pair them against your file version files. Either e.g. File_Comment_vnumber, or create a sub-directory named e.g. Comments and store the corresponding comments in the same-named File_vnumber but in the Comments sub-directory....



  • @JonB I would gladly avoid vcs as my tool is fairly simple. But the problem is, being a beginner myself i couldn't find a forum discussion regarding this topic or a similar example i could improvise on.



  • @younglegend
    Which is why I have suggested a very simple solution for you just using similarly named files. Whether that's adequate for whatever you are producing only you know.


  • Banned

    First I know that I am using PyQt5 and you are using PySide2 but often all you have to do is replace the Import reference and everything else works just fine -- Second we start by cleaning up your code base a bit and tweaking it slightly to allow for the functionality you have outlined -- as follows:

    # Never Import more than you need and definitely not entire libraries
    # however if for testing purposes you want something fast then use the
    # following formats as these set you up for drilling down to what is 
    # being used
    #from PyQt5.QtCore    import *
    #from PyQt5.QtGui     import *
    #from PyQt5.QtWidgets import *
    
    from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QGroupBox, QGridLayout
    from PyQt5.QtWidgets import QListWidget, QTextEdit, QPushButton
    
    class Dialog(QDialog):
      # Do not use Globals especially in a Class they are almost
      # never needed and always dangerous
      #  NumList = 20
        def __init__(self):
        # Do not use super( ) unless you are using it to compensate
        # for the issue it was designed for because otherwise you are
        # introducing more potential issues than you are compensating
        # for -- if you do not know that this issue is then you do not
        # need to and should not use super( )
        #    super(Dialog, self).__init__()
            QDialog.__init__(self)
            self.setWindowTitle("File Manager v1.0")
    
          # This declares NumList to be a property of Dialog and 
          # thus usable by anything within the Dialog Class
            self.NumList = 20
    
            self.createHorizontalGroupBox()
            self.createVerticalGroupBox()
    
            mainLayout = QVBoxLayout()
            mainLayout.addWidget(self.verticalGroupBox)
            mainLayout.addWidget(self.horizontalGroupBox)
    
            self.setLayout(mainLayout)
    
        def createVerticalGroupBox(self):
            self.verticalGroupBox = QGroupBox("List Files")
    
          # Group Code Segments in order of precedence as this
          # facilitates readability
            file_listbox = QListWidget()
            for i in range(self.NumList):
                file_list = ("File_v%d" % (i+1))
                file_listbox.addItem(file_list)
    
            VBox = QVBoxLayout()
            VBox.setContentsMargins(25,25,25,25)
            VBox.setSpacing(7)
            VBox.addWidget(file_listbox)
    
            self.verticalGroupBox.setLayout(VBox)
    
    # Okay variable naming should be clear and concise for instance
    # this method does not even use a Horizontal Box so why call it
    # it this as that makes no sense
        def createHorizontalGroupBox(self):
            self.horizontalGroupBox = QGroupBox("Notes")
    
    # Next short (sometimes object prefixed) variable names are easier to 
    # work with and easier to scan read later on -- extremely verbose 
    # variable names actually get hard to read as do extremely cryptic 
    # short names such as bE for an Edit Button so just enough to make it 
    # clear what the variable represents at a glance but not too much tends
    # to be the best overall
            self.teNoteEditr = QTextEdit()
            self.teNoteEditr.setPlainText("Test..")
    
            self.btnEdit = QPushButton("Edit")
            self.btnEdit.clicked.connect(self.EditText)
    
            self.btnSave = QPushButton("Save")
            self.btnSave.clicked.connect(self.SaveText)
    
            Grid = QGridLayout()
            Grid.setContentsMargins(25, 25, 25, 25)
            Grid.setSpacing(7)
            Grid.setColumnStretch(0,5)
    
            Grid.addWidget(self.teNoteEditr,0, 2, 4, 1)
            Grid.addWidget(self.btnEdit,0,5)
            Grid.addWidget(self.btnSave,1,5)
    
            self.horizontalGroupBox.setLayout(Grid)
    
        def EditText(self):
            print('You can now Edit Text')
    
        def SaveText(self):
            print('Text has been Saved')
    
    # Your program should include this almost always unless its not 
    # standalone Class that you import to something that does have this
    if __name__ == "__main__":
      #  app = QtWidgets.QApplication(sys.argv)
      # If you are not using Command Line arguments do not receive them
        MainEventThread = QApplication([])
      # If you plan to use Command Line arguments use argparser library
      # and again do not receive them via QApplication
    
        MainApp = Dialog()
        MainApp.show()
    
      #  sys.exit(dialog.exec_())
      # This is PyQt5 version of this yours was PyQt4
        MainEventThread.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

    Let me know if you get this working okay -- and then we can move forward with the next steps. Btw that was an interesting way to create those layouts and I kind of liked it.



  • @Denni-0 Thanks for such a nice explanation Denni! It looks cleaner now. And yes, switching to pyside was pretty straight forward.

    I actually had a very different kind of layout workflow before. Then i came across some examples that came with pyside packages folder which had everything i need. almost....



  • Bump..! Any help with this please? Still stuck at the same place with little to no progress..



  • @younglegend
    Help with what exactly? I thought you had copied Denni's stuff or whatever? I thought you were going to implemented what i suggested about keeping your comment in a "shadow" file for each file?

    Just to be 100% clear again. If you want a "proper" program for managing file versions + comments, you don't want to write it yourself, you should use a VCS out there like git or github. If however you want to knock up something to learn Qt, you could follow my simple suggestion, or you could get (a lot) more involved by doing it via a database from Qt....


  • Banned

    @younglegend Yes I am too confused -- what is it that you still need help with here -- please be specific in what was not covered?