QT 设置QLabel设置字体的颜色

一是使用setPalette()方法;
二是使用样式表;
三是可以使用QStyle;
四是可以在其中使用一些简单的HTML样式。
 
第一种,使用setPalette()方法如下:

QPalette pe;

pe.setColor(QPalette::WindowText, Qt::red);

QLabel *label = new QLabel(this);

label->setPalette(pe);

label->setText("Hello World");


第二种,使用样式表如下:

QLabel *label = new QLabel(this);

label->setStyleSheet("background-color: rgb(250, 0, 0);font-size:60px;color:blue");

label->setText("Hello World");


第三种,使用QStyle
 

第四种,使用一些简单的HTML格式:

QLabel *label = new QLabel(tr("Hello Qt!"));
QLabel *label = new QLabel("<h2><i>Hello</i><font color=red>Qt!</font></h2>");

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the Mac OS X styles.
Style sheets let you perform all kinds of customizations that are difficult or impossible to perform using QPalette alone

所以尽量使用方法二

猜你喜欢

转载自blog.csdn.net/technologyleader/article/details/82188395
今日推荐