Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why PYQT5 connecting multiple checkboxes does not work in a loop, but works by writing out everything manually

I am working on PyQT5 and I have the following list.

self.config_cbs = [self.gui.cb_config_0, \
                   self.gui.cb_config_1, \
                   self.gui.cb_config_2, \
                   self.gui.cb_config_3, \
                   self.gui.cb_config_4, \
                   self.gui.cb_config_5, \
                   self.gui.cb_config_6, \
                   self.gui.cb_config_7, \
                   self.gui.cb_config_8, \
                   self.gui.cb_config_9, \
                   self.gui.cb_config_10, \
                   self.gui.cb_config_11]

I want to connect all checkboxes to the same function.

Does anyone know, why does this NOT work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i, cb in enumerate(self.config_cbs):
      cb.stateChanged.connect(lambda: self.config_cb_state_changed(i))

But this does work:

self.gui.cb_config_0.stateChanged.connect(lambda: self.config_cb_state_changed(0))
self.gui.cb_config_1.stateChanged.connect(lambda: self.config_cb_state_changed(1))
self.gui.cb_config_2.stateChanged.connect(lambda: self.config_cb_state_changed(2))
...

Aren’t these two equivalent?

>Solution :

Can you try with this?

for i, cb in enumerate(self.config_cbs):
    cb.stateChanged.connect(lambda i=i: self.config_cb_state_changed(i))

If it still does not work, you can still alter your function config_cb_state_changed() and identify the checkbox which triggered the signal with self.sender() (I assume all the code is part of the same class).

...
for cb in self.config_cbs:
    cb.stateChanged.connect(self.config_cb_state_changed)
...


def config_cb_state_changed(state: bool):
    cb = self.sender()
    # If needed, you can find your index i
    i = self.config_cbs.index(cb)
    # And just to be sure...
    assert isinstance(cb, QCheckBox)
    assert cb.isChecked() == state
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading