how to change QCheckBox text label color in Qt?

后端 未结 4 759
别跟我提以往
别跟我提以往 2020-12-11 05:04

I\'m unable to change the color of Qcheckbox in QT, can somebody help me with code to change color of check box text label. I have tried

相关标签:
4条回答
  • 2020-12-11 05:46

    Looks like for some widgets you have to force using non-system "widget-engine". For checkbox it can be done by setting borders to none. So checkbox's style looks like:

    QCheckBox {
       border: none;
       color: white;
    }
    

    Similar behavior is required by other widgets. Some style-properties do not disable native look. For example QPushButton (http://doc.qt.io/qt-4.8/stylesheet-reference.html)

    0 讨论(0)
  • 2020-12-11 05:51

    I ran into this problem using various versions of Qt5 (5.2, 5.4). To do it with style sheets I had to use the Pseudo-States properties: http://doc.qt.io/qt-4.8/stylesheet-reference.html#list-of-pseudo-states

    Example:

    myCheckbox->setStyleSheet("QCheckBox:unchecked{ color: red; }QCheckBox:checked{ color: red; }");
    

    Setting both states changed the colors for me. It seems there are some oddities like this in the stylesheet and palette system so keep your eyes out for them and try a few different things (i.e. selectors, pseudo-states, etc.)

    0 讨论(0)
  • 2020-12-11 06:01

    You could use stylesheets.

    e.g:

    checkBox->setStyleSheet("QCheckBox { color: red }");
    

    For more details check the style sheets in Qt Reference and the stylesheets documentation

    0 讨论(0)
  • 2020-12-11 06:01

    This works for me:

    QPalette p = myCheckBox->palette();
    p.setColor(QPalette::Active, QPalette::WindowText, green);
    myCheckBox->setPalette(p);
    
    0 讨论(0)
提交回复
热议问题