How to change the text color of the label which is in QCheckBox?



  • i want to change the text color of label inside a checkbox without creating a customised QCheckBox.Is it possible ?I tried stylesheets .Please suggest.



  • @Sriu1
    And what did you actually try in your stylesheet?
    https://stackoverflow.com/a/35487195/489865 gives the answer for Qt 5.



  • Hi
    @JonB
    I tried
    ui->myCheckbox->setStyleSheet("QCheckBox:unchecked{ color: red; }QCheckBox:checked{ color: white; }");
    Not working my background is black.

    I want only the label text to be changed.Like in the normal qlabels where we set rich text and select color.



  • @Sriu1

    • I don't see why setting color rather than background-color style would change background color.
    • The stackoverflow post is in answer to questioner's

    can somebody help me with code to change color of check box text label.

    I take that to refer to the color (foreground) of the text. However, I leave it to you.



  • Hi

    Use
    ui->mycheckbox->setstyleSheet{ QString( " QCheckBox{ color: white; }"); }

    For indicator color use :
    QString("QCheckBox::indicator { border: 1px solid #4198EF }")

    and you can use some Icon for checked check box:

    QString("QCheckBox::indicator:checked {") +
    QString("image: url(") + checkedCheckBoxIconPath() + QString("); ") +
    QString("}");

    Also check if parent widget is overriding this styles something like setStyleSheet(QWidget { color: black; });



  • To change the text color, you can modify the palette:

    QPalette palette = myCheckBox->palette();
        palette.setColor(WindowText, QColor(Qt::red));
        myCheckBox->setPalette(palette);
    


  • Try

    setStylesheet("QCheckBox::QLabel {color: red;}")
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.