PDA

View Full Version : why changing QCheckBox's text color doesn't work?



nuliknol
7th November 2015, 18:30
I am trying to change the text color of the QCheckBox and surprisingly, nothing happens. I tried it like this first:



chk_attachments.setStyleSheet("QCheckBox { color : #767676 }");


and like this:


QPalette p;
chk_attachments.setText("Attachments");
p=chk_attachments.palette();
p.setColor(QPalette::Text, Qt::red);
chk_attachments.setPalette(p);

why my attempts are ignored by QT?

ChrisW67
8th November 2015, 04:24
#include <QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>


int main(int argc, char **argv)
{
QApplication app(argc, argv);


QCheckBox *c1 = new QCheckBox("Default colouring");
c1->setCheckState(Qt::Checked);

QCheckBox *c2 = new QCheckBox("Should be grey");
c2->setStyleSheet("QCheckBox { color : #767676 }");
c2->setCheckState(Qt::Checked);

QCheckBox *c3 = new QCheckBox("Should be red");
QPalette p = c3->palette();
p.setColor(QPalette::WindowText, Qt::red);
c3->setPalette(p);
c3->setCheckState(Qt::Checked);

QWidget w;
QVBoxLayout *l = new QVBoxLayout(&w);
l->addWidget(c1);
l->addWidget(c2);
l->addWidget(c3);
w.setLayout(l);
w.show();
return app.exec();
}


The code above works just fine on my Linux Qt4 and Qt5.
The stylesheet approach also colours the indicator, so you probably need to also style QCheckBox::indicator.
The palette approach requires the WindowText property.

nuliknol
8th November 2015, 22:10
Well, your code does not work in my QT installation. (version 5.5, built on August 18)
I see all the text of the checkboxes in black.

Weird stuff
.
Thank you for the reply, I now know this is not the problem of my coding.