0

Using QtDesigner I created a menu item (QAction) and set it to checkable and checked. The resulting code generated by pyuic5 contains

        self.actionEdit_Mode = QtWidgets.QAction(MainWindow)
        self.actionEdit_Mode.setCheckable(True)
        self.actionEdit_Mode.setChecked(True)

when I start the app and look at the menu, the checkmark appears, just as I would expect:

enter image description here

The action item triggers this function:

  def editMode(self):
    action = self.actionEdit_Mode
    print(action.isChecked())
    action.setChecked(True)
    print(action.isChecked())
    action.setChecked(False)
    print(action.isChecked())

when I click on the menu item, the output is

False
True
False

In other words, it is seeing the initial value as unchecked, even though it was set checked in the setupUI routine and appears checked in the interface. After the function returns, the menu item appears unchecked. Click again, and the function prints

True
True
False

So again it reads the inverse of what is being displayed. But what is so baffling to me, is that if I call the setChecked method then immediately read the value, I get the correct value.

What am I missing here? Thanks

Llaves
  • 683
  • 7
  • 20
  • Clicking on it changes the checked state, so this is exactly what should happen. – alec Nov 25 '21 at 02:20
  • that's surprising, but it explains everything – Llaves Nov 25 '21 at 04:15
  • @Llaves it's not unexpected, the behavior is the same as a checkable button: if you click on a checkable button, the `clicked` and `toggled` signals will be emitted with a *checked* (`True`) state if it was unchecked before (and viceversa), because you're normally interested in the new state, not the previous. The logic is the same for actions, the difference is that you don't see the action after clicking and that might seem counterintuitive, but if you add the action to a toolbar you'd see the state change, and receiving the "previous" state would be wrong. – musicamante Nov 25 '21 at 09:39

0 Answers0