【Qt学习】如何设置控件、标签、按钮 字体大小和背景颜色

如何设置控件、标签、按钮 字体大小和背景颜色?

以下提供几种方法可供参考:

一、使用setPalette()方法
二、使用样式表setStyleSheet()


第一种,使用setPalette()方法如下:

QPalette pe;
	pe.setColor(QPalette::WindowText, Qt::red);
	QLabel *label = new QLabel(this);
	label->setPalette(pe);
	label->setText("Hello World");

第二种,使用样式表setStyleSheet()方法如下:

setStyleSheet("background-color:DeepSkyBlue;font-size:40px;color:white")

依次是:背景颜色-字体大小-字体颜色

//登录按钮
    this->BtnLogin=new QPushButton("登陆",this);
    this->BtnLogin->setStyleSheet("background-color:DeepSkyBlue;font-size:40px;color:white");
    this->BtnLogin->setGeometry(185,505,400,65);

效果图:

参考:QT5如何设置QLabel中字体的颜色_N3verL4nd的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/m0_61745661/article/details/123852619
今日推荐