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

Changing the color of a QCheckBox text also changes the check-sign color



  • Hey.
    I am trying to create color changes in the label of a QCheckBox using QWidget::setStyleSheet:

    void Trigger::updateTrigger(bool state) { // this is a slot connected to the checkbox 'clicked' signal
        if (state) {
            _trigger->setStyleSheet("QCheckBox {color: red;}");
            _trigger->setText("Waiting for trigger...");
        } else {
            _trigger->setStyleSheet("QCheckBox {color: black;}");
            _trigger->setText("Check Trigger box to start");
        }
    }
    

    This almost works.
    0_1552065596104_5019b625-2ef5-4f77-8669-0058b66b0633-image.png
    See how the check sign is also red?
    I want the label, and only the label, to change its color.

    What am I doing wrong?
    Is it even possible?

    Thanks.



  • depends on how the QCheckBox is implemented and how it uses color roles from the palette.

    A work-around would be to create a subclass in which the main checkbox has no text, but that the subclass also contains a QLabel with whatever attributes you desire.


  • Lifetime Qt Champion

    Hi
    What platform is that ?
    On windows (10) it seems to use black anyway
    alt text



  • @Kent-Dorfman thanks. I will try this wa as a last resort.

    @mrjj can you remind me again how do I check what is the platform (I am running linux if that's what you meant)?

    Thanks.



  • yes, he meant operating system...and good idea to also post Qt version number used. Good ettiquitte would be to always tell what Qt version and OS you are using but that would be like training cats to dance in a chorus line.


Log in to reply