PyQt - How to connect multiple signals to the same widget

Posted by Orchainu on Stack Overflow See other posts from Stack Overflow or by Orchainu
Published on 2012-09-11T19:02:45Z Indexed on 2012/09/11 21:38 UTC
Read the original article Hit count: 189

Filed under:
|
[ ]All1           [ ]All2        

[ ]checkbox1A     [ ]checkbox1B

[ ]checkbox2A     [ ]checkbox2B

Based on the chart above, a few things need to happen:

  1. The All checkboxes only affect the on/off of the column it resides in, and checks on/off all the checkboxes in that column.
  2. All checkboxes work in pairs, so if checkbox1A is on/off, checkbox1B needs to be on/off
  3. If an All checkbox is checked on, and then the user proceeds to check off one or more checkbox in the column, the All checkbox should be unchecked, but all the checkboxes that are already checked should remain checked.

So really this is more like a chain reaction setup. If checkbox All1 is on, then chieckbox1A and 2A will be on, and because they are on, checkbox1B and 2B are also on, but checkbox All2 remains off. I tried hooking up the signals based on this logic, but only the paired logic works 100%. The All checkbox logic only works 50% of the time, and not accurately, and there's no way for me to turn off the All checkbox without turning all already checked checkboxes off.

Really really need help ... T-T

Sample code:

cbPairKeys = cbPairs.keys()
    for key in cbPairKeys:
        cbOne = cbPairs[key][0][0]
        cbTwo = cbPairs[key][1][0]
        cbOne.stateChanged.connect(self.syncCB)
        cbTwo.stateChanged.connect(self.syncCB)

def syncCB(self):               
    pairKeys = cbPairs.keys()
    for keys in pairKeys:
        cbOne = cbPairs[keys][0][0]
        cbOneAllCB = cbPairs[keys][0][4]
        cbTwo = cbPairs[keys][1][0]
        cbTwoAllCB = cbPairs[keys][1][4]

        if self.sender() == cbOne:
            if cbOne.isChecked() or cbTwoAllCB.isChecked():
                cbTwo.setChecked(True)
            else:
                cbTwo.setChecked(False)
        else:
            if cbTwo.isChecked() or cbOneAllCB.isChecked():
                cbOne.setChecked(True)
            else:
                cbOne.setChecked(False) 

EDIT

Thanks to user Avaris's help and patience, I was able to reduce the code down to something much cleaner and works 100% of the time on the 1st and 2nd desired behavior:

#Connect checkbox pairs     
cbPairKeys = cbPairs.keys()
    for key in cbPairKeys:
        cbOne = cbPairs[key][0][0]
        cbTwo = cbPairs[key][1][0]
        cbOne.toggled.connect(cbTwo.setChecked)
        cbTwo.toggled.connect(cbOne.setChecked) 

#Connect allCB and allRO signals    
cbsKeys = allCBList.keys()
    for keys in cbsKeys:
        for checkbox in allCBList[keys]:
            keys.toggled.connect(checkbox.setChecked)

Only need help on turning off the All checkbox when the user selectively turns off the modular checkboxes now

© Stack Overflow or respective owner

Related posts about python

Related posts about pyqt4