QT styleSheet——控件设置样式表

QT开发中,需要设置多种多样的控件表现形式,QT实现的styleSheet能够满足多种多样的场景,这里简单的记录下一些我常用的

  • 设置透明背景,鼠标悬浮时,设置背景色:
cpp 复制代码
pushButton->setStyleSheet("QPushButton {background: transparent;border: none;} QPushButton:hover {background-color: rgba(200, 200, 200, 255);}")
//这里设置pushButton的背景,正常情况下背景设置为透明,当有鼠标悬浮时,设置背景色rgba(200, 200, 200, 255)
  • 根据控件的property值设置对应的背景:
cpp 复制代码
   //widget是一个父控件,他的子对象可以根据property值,设置对应的样式
    widget->setStyleSheet("*[mandatoryField=\"true\"] { background-color: yellow }");
    child1= widget->findChild<QCheckBox*>("EditGroupCB");
    child2= widget->findChild<QComboBox*>("searchDepthCO");
    child1->setProperty("mandatoryField",true); // 背景会设置为黄色
    child2->setProperty("mandatoryField",true); // 背景会设置为黄色

QT官方网站关于styleSheet的介绍点击这里

相关推荐
yzp-几秒前
记录一个死锁异常--循环打印 AB go语言
开发语言·后端·golang
codealy7 分钟前
MYSQL索引失效常见场景 - 数据库性能优化
数据库·mysql·性能优化
tryxr18 分钟前
Java 中 this 关键字的使用场景
java·开发语言·类与对象·this关键字
九皇叔叔18 分钟前
MySQL数据库 意向锁(初篇)
数据库·mysql
写代码的【黑咖啡】19 分钟前
面向对象编程入门:从类与对象到构造函数
开发语言·python
阿拉伯柠檬19 分钟前
MySQL基本查询
linux·数据库·mysql·面试
沐知全栈开发20 分钟前
Perl POD 文档
开发语言
Dargon28821 分钟前
Simulink的回调函数(二)
开发语言·matlab·simulink·mbd软件开发
ICT技术最前线25 分钟前
路由策略优化基本思路和方法
开发语言·php
lly20240626 分钟前
Docker 安装 Ubuntu
开发语言