修改了wd的文章:

如何使用Qt5,设置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

所以尽量使用方法二

最新文章

  1. Windows上安装使用MongoDB(一)
  2. nginx的worker_processes,worker_cpu_affinity及worker_connections
  3. 51NOD 算法马拉松12
  4. xdebug初步
  5. js实现收缩菜单效果
  6. 基于UDP协议的多路广播地址
  7. Android发展简报
  8. iOS7 初体验
  9. 24 AIDL案例
  10. 自定义AlertDialog(仿微信)
  11. 学习笔记之form表单
  12. Java内存管理-程序运行过程(一)
  13. DER、CRT、CER、PEM格式的证书及转换
  14. webSocket开发chat application过程
  15. 发送html内容的email(转)
  16. linux fack 文件系统修复命令
  17. JavaScript设计模式与开发实践:分时函数
  18. iOS 解决上传100张图片内存奔溃问题
  19. 多线程设计模式(三):Master-Worker模式
  20. contentType 应用,(表中数据大量存在外键时使用)

热门文章

  1. Spring中常见的设计模式——代理模式
  2. Spark学习笔记(二)—— Local模式
  3. .net core 3.0 搭建 IdentityServer4 验证服务器
  4. 关于Mach-O类型文件那点事
  5. .Net Core - AgileHttp
  6. eclipse中使用postgreSQL报错( Cannot load JDBC driver class )
  7. mysql的简单命令
  8. 大白话原型模式(Prototype Pattern)
  9. JAVA大数贪心
  10. 算法笔记codeup-Contest100000568