Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Styling a QCheckBox on demand



  • 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



  • @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?



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



  • @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?



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


  • Lifetime Qt Champion

    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.



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


Log in to reply