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. Simplifying objects
Qt 6.11 is out! See what's new in the release blog

Simplifying objects

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 1.4k 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.
  • G Offline
    G Offline
    GaryN
    wrote on last edited by
    #1

    I have working on a form in QT Creator 7.0.2, using Python.

    I have the following files:

    • form.ui (created by the system)
    • Stelcor.pyproject (created by the system)
    • widget.py (created by the system)
    • definitions.py (created by myself)
    • savecsv.py (created by myself)

    I have gotten the code to work as I want it to but would like to explore any method of simplification that I cannot yet see.

    I have several lines such as these...

    self.lunit = self.window.findChild(QLineEdit, 'lunit')
    self.lfile = self.window.findChild(QLineEdit, 'lfile')
    self.kunit = self.window.findChild(QLineEdit, 'kunit')
    self.sfile = self.window.findChild(QLineEdit, 'sfile')
    self.junit = self.window.findChild(QLineEdit, 'junit')
    self.ofile = self.window.findChild(QLineEdit, 'ofile')
    self.unitz = self.window.findChild(QLineEdit, 'unitz')
    self.filez = self.window.findChild(QLineEdit, 'filez')
    

    The QLineEdit names on the form are necessary as they are; they help me relate this form with another program I am working on.

    I am wondering if there is a way I can set up the self.lunit et al in such a way that I can loop through them, rather than have lots of lines, as I currently do.

    As I say, the QLineEdit names need to remain, but the self.xxxx can be anything - I am not emotional about these.

    Any ideas?

    JonBJ 1 Reply Last reply
    0
    • G GaryN

      I have working on a form in QT Creator 7.0.2, using Python.

      I have the following files:

      • form.ui (created by the system)
      • Stelcor.pyproject (created by the system)
      • widget.py (created by the system)
      • definitions.py (created by myself)
      • savecsv.py (created by myself)

      I have gotten the code to work as I want it to but would like to explore any method of simplification that I cannot yet see.

      I have several lines such as these...

      self.lunit = self.window.findChild(QLineEdit, 'lunit')
      self.lfile = self.window.findChild(QLineEdit, 'lfile')
      self.kunit = self.window.findChild(QLineEdit, 'kunit')
      self.sfile = self.window.findChild(QLineEdit, 'sfile')
      self.junit = self.window.findChild(QLineEdit, 'junit')
      self.ofile = self.window.findChild(QLineEdit, 'ofile')
      self.unitz = self.window.findChild(QLineEdit, 'unitz')
      self.filez = self.window.findChild(QLineEdit, 'filez')
      

      The QLineEdit names on the form are necessary as they are; they help me relate this form with another program I am working on.

      I am wondering if there is a way I can set up the self.lunit et al in such a way that I can loop through them, rather than have lots of lines, as I currently do.

      As I say, the QLineEdit names need to remain, but the self.xxxx can be anything - I am not emotional about these.

      Any ideas?

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

      @GaryN
      If you are happy to have an array/list for your self... variable (instead of individual variables as you have now), you can use a Python enumerate loop to set up this array from a list of the names of the QLineEdits conveniently.

      G 2 Replies Last reply
      0
      • JonBJ JonB

        @GaryN
        If you are happy to have an array/list for your self... variable (instead of individual variables as you have now), you can use a Python enumerate loop to set up this array from a list of the names of the QLineEdits conveniently.

        G Offline
        G Offline
        GaryN
        wrote on last edited by
        #3

        @JonB
        Thanks for this. I did attempt this using a dictionary but this failed miserably!

        Might I ask for an example? Sorry, did try but nothing worked.

        1 Reply Last reply
        0
        • JonBJ JonB

          @GaryN
          If you are happy to have an array/list for your self... variable (instead of individual variables as you have now), you can use a Python enumerate loop to set up this array from a list of the names of the QLineEdits conveniently.

          G Offline
          G Offline
          GaryN
          wrote on last edited by
          #4

          @JonB
          Just to show what I tried (based upon information I found elsewhere):

          def initialise(self):
          
              dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit', 'ofile', 'unitz', 'filez']
              fv_dict = {}
          
              for dataItem in dataItems:
                  self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
          

          This generates the error:

          AttributeError: 'Form' object has no attribute 'fv_dict'
          
          JonBJ 1 Reply Last reply
          0
          • G GaryN

            @JonB
            Just to show what I tried (based upon information I found elsewhere):

            def initialise(self):
            
                dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit', 'ofile', 'unitz', 'filez']
                fv_dict = {}
            
                for dataItem in dataItems:
                    self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
            

            This generates the error:

            AttributeError: 'Form' object has no attribute 'fv_dict'
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @GaryN said in Simplifying objects:

            fv_dict = {}
            self.fv_dic[]

            Read the error message, think about it. This is basic Python.

            Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with enumerate, yours is fine.

            G 2 Replies Last reply
            0
            • JonBJ JonB

              @GaryN said in Simplifying objects:

              fv_dict = {}
              self.fv_dic[]

              Read the error message, think about it. This is basic Python.

              Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with enumerate, yours is fine.

              G Offline
              G Offline
              GaryN
              wrote on last edited by
              #6

              @JonB
              Yes, sorry. Brain just clouded over!

              All solved now. :)

              1 Reply Last reply
              0
              • JonBJ JonB

                @GaryN said in Simplifying objects:

                fv_dict = {}
                self.fv_dic[]

                Read the error message, think about it. This is basic Python.

                Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with enumerate, yours is fine.

                G Offline
                G Offline
                GaryN
                wrote on last edited by
                #7

                @JonB

                Further to this (and to confirm I am not the idiot I might first appear), the code has been successfully reduced to 21 lines; far better than the 300 I had before.

                Thank you.

                1 Reply Last reply
                1

                • Login

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