PyQt6 QCheckBox, different action when text/box is clicked.
-
Hi,
I am trying to make a QCheckbox where I seperate between clicking on the checkbox "text" and the checkbox "box".I might be missing something, but I have not found support for this in the existing functions (yet).
In an attempt to solve it I created a "Custom Checkbox" that includes the mouse press coordinates:
class CustomCheckBox(QCheckBox):
def init(self,text):
super().init(text)
self.lastClickedEvent = None
def mousePressEvent(self, event: QMouseEvent):
self.lastClickedEvent = event
super().mousePressEvent(event)If I could get the coordinates for the text, and the coordinates for the box when its clicked I believe I could use something similar to:
checkboxRect = checkBox.contentsRect()
clickPosition = checkBox.lastClickedEvent.position().toPoint()if checkboxRect.contains(clickPosition):
print(f"Clicked on the {checkBoxID} checkbox box")
else:
print(f"Clicked on the {checkBoxID} checkbox text")My problem, I think, is that contentsRect gives the coordinates for the whole checkbox widget and it does not seperate between text and box.
I would appreciate any ideas for how to solve this.
-
Hi and welcome to devnet,
It would likely be simpler to associate your QCheckBox with a QLabel so you can separately handle whatever needs to happen differently.
-
@spinbean said in PyQt6 QCheckBox, different action when text/box is clicked.:
I am trying to make a QCheckbox where I seperate between clicking on the checkbox "text" and the checkbox "box".
I think you will find you should not do this. For consistent interface end users will expect clicking either will have same behaviour (checkbox state toggle), that is how 99% of applications work.