Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Weird behavior of checkable action toggling in a QMenu
Qt 6.11 is out! See what's new in the release blog

Weird behavior of checkable action toggling in a QMenu

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 776 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by
    #1
        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).

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by
      #2

      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)
      
      

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved