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的介绍点击这里

相关推荐
Byron Loong9 小时前
【c#】Bitmap释放之 大象与蚊子
开发语言·c#
音符犹如代码9 小时前
Java动态线程池:避坑原生线程池,吃透Dynamic-TP,手写一个Demo
java·开发语言
喜欢就别9 小时前
平均风险:Rényi DP 和零集中 DP
开发语言·r语言
无足鸟ICT9 小时前
【RHCA+】bash命令
linux·开发语言·bash
方华世界10 小时前
企业级Java AI Agent应用平台
java·开发语言·人工智能
阿演10 小时前
DataDjinn v0.2.11:SQL 编辑、AI 协作和表格操作继续打磨
数据库·人工智能·sql
ujainu小11 小时前
原生性能优化:6变量实现高效桥接
开发语言·华为·性能优化·harmonyos
甄同学11 小时前
第十七篇:Bash Executor命令执行器,安全运行Shell命令
开发语言·安全·bash
张32311 小时前
Go语言基础 Map 函数值 闭包
开发语言·golang
杜子不疼.11 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++