QAction.isChecked seems to report inverted value

62 Views Asked by At

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

0

There are 0 best solutions below