QLabel: set color of text and background

后端 未结 6 1639
清歌不尽
清歌不尽 2020-11-30 20:03

How do I set color of text and background of a QLabel ?

6条回答
  •  Happy的楠姐
    2020-11-30 20:27

    You can use QPalette, however you must set setAutoFillBackground(true); to enable background color

    QPalette sample_palette;
    sample_palette.setColor(QPalette::Window, Qt::white);
    sample_palette.setColor(QPalette::WindowText, Qt::blue);
    
    sample_label->setAutoFillBackground(true);
    sample_label->setPalette(sample_palette);
    sample_label->setText("What ever text");
    

    It works fine on Windows and Ubuntu, I haven't played with any other OS.

    Note: Please see QPalette, color role section for more details

提交回复
热议问题