Weird behavior of checkable action toggling in a QMenu



  •     def _buildSceneContextMenu(self):
            menu = QMenu()
            
            place_menu = menu.addMenu('Place')
            place_led = place_menu.addAction('LED')
            place_button = place_menu.addAction('Button')
            
            self._placeActions = [place_led, place_button]
            
            for action in self._placeActions:
                action.setCheckable(True)
                action.triggered.connect(lambda: self._placeActionTriggered(action))
                    
            place_led.setChecked(True)
            
            # place_led.triggered.connect(lambda b: None)
            return menu
        
        def _placeActionTriggered(self, action):
            for action1 in self._placeActions:
                if action != action1:
                    action1.setChecked(False)
                    
            action.setChecked(True)
    

    This doesn't work as expected. LED starts off checked, and I can click Button and it gets checked, but I can't switch back to LED. It is not a case of the lambda's getting mixed up for which you would need another function to connect the triggered signal - I checked the address of action parameter in _placeActionTriggered. I can't use a QActionGroup, because it simply did not work (no radios or checks presented).



  • No, it is a case of the signal connections getting mixed up, because this fixed it:

            self._placeActions = [place_led, place_button]
            
            def connectPlaceActionTrigger(action):
                action.triggered.connect(lambda: self._placeActionTriggered(action))
            
            for action in self._placeActions:
                action.setCheckable(True)
                connectPlaceActionTrigger(action)
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.