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. Activating a button
Forum Updated to NodeBB v4.3 + New Features

Activating a button

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 400 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 8 Jun 2022, 09:12 last edited by
    #1

    I have created a form (form.ui) using the drag and drop features of QT Creator.

    To access these objects, I define a local object within my code and, thanks to the help of others, have done so through a loop:

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

    The rest of this subroutine reads in a CSV textfile and assigns the values from that CSV file to each of the QLineEdits on the form (I have shortened the number of QLineEdits above to 5; there are, in fact, over 60).

    I have gotten the system to trigger an event when any of the textboxes have their contents changed by the user (given with the connect shown above and running the following code).

    def textedited(text):
        
        print(text)
    

    So far, so good.

    I have added two buttons to the form using the drag and drop interface; calling them saveChanges and discardChanges. I have disabled both buttons.

    I want to have the buttons enabled if any of the QLineEdit's have their contents changed; thus using the triggered event I have shown above.

    I tried declaring the objects in the subroutine above, changing the single parameter to 'self' and adding this to the connect statement previously...

    def initialise(self):
    
        dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit']
        self.fv_dict = {}
    
        for dataItem in dataItems:
            self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
            self.fv_dict[dataItem].textEdited.connect(textedited(self))
    ....
    
    def textedited(self):
    
        self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
        self.discardButton = self.window.findChild(QPushButton, 'DiscardChanges')
    

    This appears to run fine; no errors. Not convinced I should have to declare these objects each and every time the contents of a QLineEdit is changed, but this appears to be the nature of the beast (given my limited understanding at present).

    I then add a setEnabled to the end of that function...

    def textedited(self):
    
        self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
        self.discardButton = self.window.findChild(QPushButton, 'DiscardChanges')
    
        self.saveButton.setEnabled(True)
    

    And now this won't run:

    AttributeError: 'NoneType' object has no attribute 'setEnabled'
    

    Where am I going wrong?

    J 1 Reply Last reply 8 Jun 2022, 11:09
    0
    • G GaryN
      8 Jun 2022, 09:12

      I have created a form (form.ui) using the drag and drop features of QT Creator.

      To access these objects, I define a local object within my code and, thanks to the help of others, have done so through a loop:

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

      The rest of this subroutine reads in a CSV textfile and assigns the values from that CSV file to each of the QLineEdits on the form (I have shortened the number of QLineEdits above to 5; there are, in fact, over 60).

      I have gotten the system to trigger an event when any of the textboxes have their contents changed by the user (given with the connect shown above and running the following code).

      def textedited(text):
          
          print(text)
      

      So far, so good.

      I have added two buttons to the form using the drag and drop interface; calling them saveChanges and discardChanges. I have disabled both buttons.

      I want to have the buttons enabled if any of the QLineEdit's have their contents changed; thus using the triggered event I have shown above.

      I tried declaring the objects in the subroutine above, changing the single parameter to 'self' and adding this to the connect statement previously...

      def initialise(self):
      
          dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit']
          self.fv_dict = {}
      
          for dataItem in dataItems:
              self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
              self.fv_dict[dataItem].textEdited.connect(textedited(self))
      ....
      
      def textedited(self):
      
          self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
          self.discardButton = self.window.findChild(QPushButton, 'DiscardChanges')
      

      This appears to run fine; no errors. Not convinced I should have to declare these objects each and every time the contents of a QLineEdit is changed, but this appears to be the nature of the beast (given my limited understanding at present).

      I then add a setEnabled to the end of that function...

      def textedited(self):
      
          self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
          self.discardButton = self.window.findChild(QPushButton, 'DiscardChanges')
      
          self.saveButton.setEnabled(True)
      

      And now this won't run:

      AttributeError: 'NoneType' object has no attribute 'setEnabled'
      

      Where am I going wrong?

      J Offline
      J Offline
      JonB
      wrote on 8 Jun 2022, 11:09 last edited by
      #2

      @GaryN said in Activating a button:

      AttributeError: 'NoneType' object has no attribute 'setEnabled'

      So the

      self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
      

      returned None, i.e. no QPushButton named SaveChanges was found.... Did you create the button, put it in self.window, and what did you set its objectName to?

      G 1 Reply Last reply 12 Jun 2022, 14:41
      1
      • J JonB
        8 Jun 2022, 11:09

        @GaryN said in Activating a button:

        AttributeError: 'NoneType' object has no attribute 'setEnabled'

        So the

        self.saveButton = self.window.findChild(QPushButton, 'SaveChanges')
        

        returned None, i.e. no QPushButton named SaveChanges was found.... Did you create the button, put it in self.window, and what did you set its objectName to?

        G Offline
        G Offline
        GaryN
        wrote on 12 Jun 2022, 14:41 last edited by
        #3

        @JonB

        Capitalised D and S!

        Sometimes just having someone asking questions makes you look again differently.

        Thank you.

        1 Reply Last reply
        0

        1/3

        8 Jun 2022, 09:12

        • Login

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