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

相关推荐
硅基动力AI9 小时前
如何判断一个关键词值不值得做?
java·前端·数据库
学无止境_永不停歇9 小时前
十一、C++11列表初始化、右值引用和移动语义
开发语言·c++
阿里嘎多学长10 小时前
2026-02-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
爱看书的小沐10 小时前
【小沐学CAD】基于OCCT读取和显示STEP模型文件(QT、MFC、glfw)
qt·mfc·opengl·stp·step·opencascade·occt
mjhcsp10 小时前
C++ 背包DP解析
开发语言·c++
新缸中之脑10 小时前
从零实现AI代理的长期记忆
数据库·人工智能
尘缘浮梦10 小时前
协程asyncio入门案例 2
开发语言·python
juleskk10 小时前
2.15 复试训练
开发语言·c++·算法
一个处女座的程序猿O(∩_∩)O11 小时前
Python面向对象的多态特性详解
开发语言·python
清水白石00811 小时前
Fixture 的力量:pytest fixture 如何重新定义测试数据管理
数据库·python·pytest