I have a GUI with two radio buttons inside a QButtonGroup container, which itself is inside a QGroupBox (this button group needs a right click context menu, and since a QButtonGroup doesn’t have a visual representation it doesn’t seem to have a setContextMenuPolicy method.) The following code snippet is supposed to update the state of the mutually-exclusive buttons in response to incoming data from a serial link:
elif widgetName in self.buttonBoxDict:
buttonGroup = getattr(self.ui, self.buttonBoxDict[widgetName])
checkedButton = buttonGroup.checkedButton()
checkedButtonName = str(checkedButton.objectName())
if value >= self.onValue and self.buttonDict[checkedButtonName][1] == self.offValue:
checkedButton.toggle()
assert(not checkedButton.isChecked())
self.windowHandler.buttonChanged(self, self.onValue, cc)
elif value < self.onValue and self.buttonDict[checkedButtonName][1] == self.onValue:
checkedButton.toggle()
self.windowHandler.buttonChanged(self, self.offValue, cc)
Unfortunately this isn’t working, I know initially the button selected here is checked, but the state of the button in the GUI never changes, and the assertion always fails even though the code seems to be executing properly. Any ideas as to why this might be going wrong?
The problem you have is based on the fact that you are using a QButtonGroup that is set to be exclusive. If you take a look at the documentation you will see that it states the following:
I don’t know the logic of you application but if you are using an exclusive button group you will need to set a diff button to be checked or do not use an exclusive group and enforce the checks yourself.
The following is a small example of what you are seeing: