Search code examples
pythoncheckboxpyqt4

How to check stateChanged in a dynamic checkbox list in PyQt4


So I'm adding checkboxes from a list in PyQt4. But I can't figure a way to use stateChanged for each one of them in my Window class.

This is the function to add them from the list elements:

  def addCheckbox(self):
        colunas = Graphic(self.caminho).getColunas()
        for col in colunas:
            c = QtGui.QCheckBox(col, self, objectName=col)
            self.layout.addWidget(c)

I've tried to get their object names in a while True and use with:

self.name.stateChanged.connect(self.clickBox)

But that just freezes the code.

How do I do that?


Solution

  • You could try the following:

    def clickBoxStateChanged(self, objectName):
        def stateChanged(state):
            print(objectName)
            print(state)
        return stateChanged
    

    Then, in your loop:

      for col in colunas:
            c = QtGui.QCheckBox(col, self, objectName=col)
            c.stateChanged.connect(self.clickBoxStateChanged(col))
            self.layout.addWidget(c)