Results 1 to 3 of 3

Thread: why changing QCheckBox's text color doesn't work?

  1. #1
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default why changing QCheckBox's text color doesn't work?

    I am trying to change the text color of the QCheckBox and surprisingly, nothing happens. I tried it like this first:

    Qt Code:
    1. chk_attachments.setStyleSheet("QCheckBox { color : #767676 }");
    To copy to clipboard, switch view to plain text mode 

    and like this:
    Qt Code:
    1. chk_attachments.setText("Attachments");
    2. p=chk_attachments.palette();
    3. p.setColor(QPalette::Text, Qt::red);
    4. chk_attachments.setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    why my attempts are ignored by QT?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: why changing QCheckBox's text color doesn't work?

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QCheckBox>
    4. #include <QVBoxLayout>
    5.  
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication app(argc, argv);
    10.  
    11.  
    12. QCheckBox *c1 = new QCheckBox("Default colouring");
    13. c1->setCheckState(Qt::Checked);
    14.  
    15. QCheckBox *c2 = new QCheckBox("Should be grey");
    16. c2->setStyleSheet("QCheckBox { color : #767676 }");
    17. c2->setCheckState(Qt::Checked);
    18.  
    19. QCheckBox *c3 = new QCheckBox("Should be red");
    20. QPalette p = c3->palette();
    21. p.setColor(QPalette::WindowText, Qt::red);
    22. c3->setPalette(p);
    23. c3->setCheckState(Qt::Checked);
    24.  
    25. QVBoxLayout *l = new QVBoxLayout(&w);
    26. l->addWidget(c1);
    27. l->addWidget(c2);
    28. l->addWidget(c3);
    29. w.setLayout(l);
    30. w.show();
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. The following user says thank you to ChrisW67 for this useful post:

    nuliknol (8th November 2015)

  4. #3
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Re: why changing QCheckBox's text color doesn't work?

    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.

Similar Threads

  1. Replies: 4
    Last Post: 8th September 2011, 08:22
  2. Changing QPalette Highlight doesn't work on Windows 7
    By BinbinDesbois in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2011, 11:19
  3. Replies: 0
    Last Post: 4th March 2011, 19:18
  4. Replies: 4
    Last Post: 2nd December 2010, 14:57
  5. Changing the text color of a QTreeView leaf.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2006, 23:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.