Activating a button
-
wrote on 8 Jun 2022, 09:12 last edited by
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?
-
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?
wrote on 8 Jun 2022, 11:09 last edited by@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. noQPushButton
namedSaveChanges
was found.... Did you create the button, put it inself.window
, and what did you set itsobjectName
to? -
@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. noQPushButton
namedSaveChanges
was found.... Did you create the button, put it inself.window
, and what did you set itsobjectName
to?
1/3