Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt6 QCheckBox, different action when text/box is clicked.

PyQt6 QCheckBox, different action when text/box is clicked.

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 720 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    spinbean
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S spinbean

        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.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @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.

        1 Reply Last reply
        1

        • Login

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