Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Styling a QCheckBox on demand
QtWS25 Last Chance

Styling a QCheckBox on demand

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.0k Views
  • 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.
  • D Offline
    D Offline
    donquibeats
    wrote on last edited by
    #1

    Hello

    I've got this simple bit of PyQt5 code:

    def set_highlight_on_widget(boxRef, highlighted):
        css = "border-width: 1px"
        if highlighted:
            css = "border-color: red"
        boxRef.setStyleSheet(css)
    

    When the widget that's passed (boxRef) is a QLineEdit or a QPlainTextEdit, it's working great. If the widget's content is regarded as OK then the box's border matches the overall stylesheet, but if there is a problem in the box, the border of the box is highlighted red.

    My problem is, I want this to work the same with a QCheckBox as well- making the border go red (highlighted=True) if code elsewhere in the app decides that the checkbox content needs the user's attention.

    But if I pass a QCheckBox as the first parameter and True as the second to the above function, nothing happens. There's no error, but the border colour doesn't change when it should.

    Please can somebody tell me what I should be doing with the styling so that the QCheckBox border can be highlighted red on demand?

    (I'm going to have the same question soon regarding QComboBox and QListBox items as well, so I'm hoping there might be a one-size-fits-all solution, but perhaps there isn't?)

    Thanks

    JonBJ 1 Reply Last reply
    0
    • D donquibeats

      Hello

      I've got this simple bit of PyQt5 code:

      def set_highlight_on_widget(boxRef, highlighted):
          css = "border-width: 1px"
          if highlighted:
              css = "border-color: red"
          boxRef.setStyleSheet(css)
      

      When the widget that's passed (boxRef) is a QLineEdit or a QPlainTextEdit, it's working great. If the widget's content is regarded as OK then the box's border matches the overall stylesheet, but if there is a problem in the box, the border of the box is highlighted red.

      My problem is, I want this to work the same with a QCheckBox as well- making the border go red (highlighted=True) if code elsewhere in the app decides that the checkbox content needs the user's attention.

      But if I pass a QCheckBox as the first parameter and True as the second to the above function, nothing happens. There's no error, but the border colour doesn't change when it should.

      Please can somebody tell me what I should be doing with the styling so that the QCheckBox border can be highlighted red on demand?

      (I'm going to have the same question soon regarding QComboBox and QListBox items as well, so I'm hoping there might be a one-size-fits-all solution, but perhaps there isn't?)

      Thanks

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

      @donquibeats

      • On a QCheckBox it's not clear to me whether you want the border or the indicator rectangle which I don't think is same, e.g. read through https://stackoverflow.com/questions/30693600/changing-qcheckbox-indicator-rectangle-color

      • css = "border-color: red" You sure you don't intend += (with a ;)? Yours will overwrite the css = "border-width: 1px", so it might not have a border width and then it won't be visible, looks to me like you attend both of these attributes to be set?

      1 Reply Last reply
      1
      • D Offline
        D Offline
        donquibeats
        wrote on last edited by
        #3

        Thanks for the prompt response.

        With the border or the indicator, my preference would be for the border to change color rather than the indicator, as that would be consistent with the other items on the forms. However, at this stage, I'd take any workable colour change that could indicate a problem to the user- border, indicator, or anything else.

        I'd spotted that link but I haven't been able to find a successful way of translating it into PyQt5 code. How am I meant to attach styling to a specific CSS sub-item of a widget? For example boxRef.indicator.setStyleSheet(css) throws an error.

        Thanks for pointing out the CSS compounding issue. It wasn't critical to the problem (unfortunately) but sorting that out allows consistency in the border width.

        JonBJ 1 Reply Last reply
        0
        • D donquibeats

          Thanks for the prompt response.

          With the border or the indicator, my preference would be for the border to change color rather than the indicator, as that would be consistent with the other items on the forms. However, at this stage, I'd take any workable colour change that could indicate a problem to the user- border, indicator, or anything else.

          I'd spotted that link but I haven't been able to find a successful way of translating it into PyQt5 code. How am I meant to attach styling to a specific CSS sub-item of a widget? For example boxRef.indicator.setStyleSheet(css) throws an error.

          Thanks for pointing out the CSS compounding issue. It wasn't critical to the problem (unfortunately) but sorting that out allows consistency in the border width.

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

          @donquibeats
          PyQt5 is exactly the same as C++, give or take a couple of semi-colons. What link have you spotted that shows anything like boxRef.indicator.setStyleSheet(css)? Was it code (like you've written), or was it just some CSS styling for the indicator, which is not a coding thing, you specify it in the stylesheet syntax?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            donquibeats
            wrote on last edited by
            #5

            I didn't find any documentation that gave me PyQt5-ready code for the styling. The example I gave was a stab in the dark.

            I am new to PyQt5 so please bear with me.

            I would be very appreciative of an example of how to form the CSS so it refers to the indicator (or ideally the border) rather than the whole object, because my attempts so far have had no effect.

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

              Hi,

              There's nothing fundamentally different between the C++ and Python with regard to the stylesheet handling.

              See the Customizing QCheckBox part of the stylesheet examples.

              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
              1
              • D Offline
                D Offline
                donquibeats
                wrote on last edited by
                #7

                Thanks. What was throwing me was that CSS border-radius was being applied to the QCheckBox, but border-color was being ignored when on other items such as QListWidgets, border-color was valid.

                With QCheckBox items I've settled with a much simpler color: red to indicate a problem. It's nice and clear and it works.

                1 Reply Last reply
                0

                • Login

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