How to associate objects in PyQt?

Hello!

I'm developing a form using PyQt. I do not understand how objects connect to one another.

Example: I Have a QLineEdit and QCheckBox. When the status indicates this QCheckBox and QLineEdit becomes available or unavailable for input.

Now I use here such construction:

self.MyChkbox.stateChanged.connect(
 lambda state: self.MyLineEdit.setReadOnly(
 True if state == 2 else False
)
)


But you cannot, for example, passing in a loop for all the checkboxes to secondentity each with its LINEITEM and to connect hundred checkboxes hundred times I have to repeat this construction every time clearly indicating a new field.
In my view it should be possible to group widgets to each checkbox you can give the command to change the status of your widget.

Question: how do I avoid obvious names or references to objects in your code? Is there some kind of grouping widget that implements their relationship for me? What to read?
April 3rd 20 at 17:29
2 answers
April 3rd 20 at 17:31
class MyCheckBox(QCheckBox):
 def __init__(self, target_edit:QLineEdit, parent):
super().__init__(parent)
 self._target_edit = target_edit
self.stateChanged.connect(self._stateChanged_slot)

 def _stateChanged_slot(self):
 self._target_edit.setReadOnly(not self._target_edit.isReadOnly())
April 3rd 20 at 17:33
Yet managed to solve through the assignment of custom attributes (properties) in each checkbox. The attribute indicates the field name that applies to this checkbox.

During the construction of the form, set the attributes like this:

self.MyChkbox.setProperty('field_name', 'MyLineEdit')


Then when the window is initialized, do so:

for checkbox in self.findChildren(QCheckBox): # For all checkboxes of a form
 if checkbox.property('field_name'): # If the checkbox has a property 'field_name'
 checkbox.stateChanged.connect( # secondentity to change the status of checkbox function
 # Search function field is the name of the object contained in the property of the sender (checkbox)
 lambda state : self.findChild(QLineEdit, self.sender().property('field_name')).setReadOnly( 
 True if state == 2 else False # to Set the corresponding availability status to enter
)
 )


I would be very happy to any other suggestions. Especially the reference to pulp fiction.

Find more questions by tags PythonUser interfaceQtPyQt