3

I found the same question there: how to change QCheckBox text label color in Qt?

But unfortunately, none of it work for me on a mac.

On Linux and Windows, by default the text of a QWidget (QLabel, QCheckBox, QRadioButton) is white. On the mac, it's black. Unfortunately this cause issues in my screen as the text is unreadable (I have black background)..

I have derived the QCheckBox class so in the constructor you get:

class MPUBLIC MythCheckBox: public QCheckBox
{
    Q_OBJECT

  public:
    MythCheckBox(QWidget *parent = 0, const char *name = "MythCheckBox")
        : QCheckBox(parent)
    {
        setObjectName(name);
#ifdef Q_OS_MAC
//        setStyleSheet("QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");
        QPalette p = palette();
        p.setColor(QPalette::WindowText, Qt::white);
        setPalette(p);
#endif
    };

If I use stylesheets like:

setStyleSheet("QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");

then my text is white as I want, but the checkmark itself becomes invisible...

If I use the 2nd method:

QPalette p = palette();
p.setColor(QPalette::WindowText, Qt::white);
setPalette(p);

The checkbox text does become white, and the checkmark itself is still black, it looks like it works. But if I ever move the focus to the QCheckBox, the text becomes black again, and it will remain black forever.

I tried also:

QPalette p = palette();
p.setColor(QPalette::Active, QPalette::WindowText, Qt::white);
p.setColor(QPalette::Inactive, QPalette::WindowText, Qt::white);
setPalette(p);

Mind you, I only get this weird behavior on the mac; if I try the same code in Linux (with different colors like red), then everything behaves like I want it to.

Any ideas on how to change the color of a QCheckBox's text, and only the text?

Community
  • 1
  • 1
jyavenard
  • 2,142
  • 1
  • 26
  • 35
  • You can try to specify which part of widget must be modified by new style sheet. For example, if your checkbox name is MythCheckBox_0 you can call MythCheckBox_0.setStyleSheet("#MythCheckBox_0{color : white;}"); or something like this. – Ilya Mar 31 '14 at 12:37
  • Try like you did with style sheets, but without redefining the the css for QCheckBox::indicator `setStyleSheet("QCheckBox { color : white; });` – Rud Limaverde Mar 31 '14 at 12:48
  • Do you have QDesigner at your hand? If I have problems with the palette color i use that to simply play around with the colors and see if that solves my problem – Bowdzone Mar 31 '14 at 14:11
  • I do not use QDesigner, it's all coded. Rud Limaverde: when I do so (redefining the color for the whole QCheckBox), then the checkbox appear as white (e.g. you can't see it anymore)... – jyavenard Apr 01 '14 at 02:22

1 Answers1

1

For some widgets you have to disable a native look (appearance). For example QPushButton (http://doc.qt.io/qt-4.8/stylesheet-reference.html). So, for checkbox you can try to set borders. So checkbox's style looks like:

QCheckBox {
   border: none;
   color: white;
}

Works for me.

Mnt DXM
  • 168
  • 1
  • 7