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

相关推荐
聆听。。花开雨落8 小时前
mybatis的typeHandler 作用
数据库·mybatis
星栈独行8 小时前
翻完 Pi 源码:它和 Codex、Claude Code 有何不同
开发语言·javascript·人工智能·程序人生
pxzsky8 小时前
PG17数据库安装中分分词插件:pg_jieba
数据库·postgresql·pg_jieba
qq_448011168 小时前
C语言中的变量和函数的定义与声明
android·c语言·开发语言
她说可以呀9 小时前
Redis哨兵
数据库·redis·bootstrap
霸道流氓气质9 小时前
KMS 密钥管理服务(Key Management Service)原理与实践
linux·服务器·数据库
kirs_ur9 小时前
CXL(Compute Express Link)— 内存扩展的未来
服务器·数据库·性能优化
孫治AllenSun10 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm
c2385611 小时前
把 C++ 内存分配拆透:new 与 malloc 的三层血缘
开发语言·c++·算法