当前位置: 首页 > news >正文

【Qt】qss语法详解

QSS (Qt Style Sheets) 语法格式详解

QSS 是 Qt 的样式表语言,类似于 CSS,用于自定义 Qt 应用程序的外观。以下是 QSS 的完整语法格式说明:

基本语法结构

selector {property: value;property: value;...
}

1. 选择器 (Selectors)

基本选择器

  • 类型选择器:匹配指定类型的所有控件

    QPushButton { color: red; }
    
  • 类选择器:匹配指定类名的控件

    .QPushButton { background: blue; }
    
  • ID 选择器:匹配指定 objectName 的控件

    QPushButton#okButton { font-weight: bold; }
    
  • 通配选择器:匹配所有控件

    * { font-family: "Arial"; }
    

组合选择器

  • 后代选择器:匹配包含在另一个控件中的控件

    QDialog QPushButton { color: green; }
    
  • 子选择器:匹配直接子控件

    QDialog > QPushButton { padding: 5px; }
    

状态选择器

  • 伪状态:匹配控件的特定状态

    QPushButton:hover { background: yellow; }
    QCheckBox:checked { color: white; }
    QLineEdit:disabled { opacity: 0.5; }
    
  • 状态组合:多个状态同时满足

    QPushButton:hover:checked { background: orange; }
    

2. 属性和值

常用属性

  • 尺寸和边距

    min-width: 100px;
    max-height: 50px;
    margin: 5px;
    padding: 10px;
    
  • 背景

    background: red;
    background-color: #ff0000;
    background-image: url(:/images/bg.png);
    
  • 边框

    border: 1px solid black;
    border-radius: 5px;
    border-top: 2px dashed blue;
    
  • 字体

    font: bold 14px "Arial";
    font-family: "Microsoft YaHei";
    font-size: 12pt;
    color: #333333;
    
  • 其他

    opacity: 0.8;
    spacing: 5px;
    qproperty-alignment: AlignCenter;
    

特殊属性

  • 子控件:样式化复杂控件的部分

    QComboBox::drop-down { image: url(dropdown.png); }
    QSpinBox::up-button { width: 20px; }
    
  • 状态图像

    QPushButton:checked {image: url(checked.png);
    }
    

3. 盒模型

QSS 使用标准的 CSS 盒模型:

+---------------------------+
|          margin           |
|  +---------------------+  |
|  |       border        |  |
|  |  +---------------+  |  |
|  |  |    padding    |  |  |
|  |  |  +--------+   |  |  |
|  |  |  | content|   |  |  |
|  |  |  +--------+   |  |  |
|  |  +---------------+  |  |
|  +---------------------+  |
+---------------------------+

4. 渐变和颜色

/* 线性渐变 */
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 white, stop:1 black);/* 径向渐变 */
background: qradialgradient(cx:0.5, cy:0.5, radius: 0.5,fx:0.5, fy:0.5, stop:0 white, stop:1 black);/* 颜色 */
color: rgba(255, 0, 0, 128);  /* 带透明度 */

5. 常用伪状态

伪状态描述
:hover鼠标悬停
:pressed鼠标按下
:checked选中状态
:disabled禁用状态
:enabled启用状态
:focus获得焦点
:unchecked未选中状态
:indeterminate不确定状态(如三态复选框)
:open打开状态(如QComboBox)
:closed关闭状态
:on开关控件处于"on"状态
:off开关控件处于"off"状态

6. 示例代码

/* 主窗口背景 */
QMainWindow {background: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #a6a6a6, stop:1 #666666);
}/* 按钮样式 */
QPushButton {background-color: #3498db;color: white;border: 2px solid #2980b9;border-radius: 5px;padding: 5px 10px;
}QPushButton:hover {background-color: #2980b9;
}QPushButton:pressed {background-color: #1f618d;
}/* 禁用按钮 */
QPushButton:disabled {background-color: #bdc3c7;border-color: #95a5a6;
}/* 文本框样式 */
QLineEdit {border: 1px solid #bdc3c7;border-radius: 3px;padding: 3px;background: white;
}QLineEdit:focus {border: 1px solid #3498db;
}/* 复选框样式 */
QCheckBox {spacing: 5px;
}QCheckBox::indicator {width: 15px;height: 15px;
}QCheckBox::indicator:checked {image: url(:/images/checked.png);
}/* 组合框下拉箭头 */
QComboBox::drop-down {subcontrol-origin: padding;subcontrol-position: top right;width: 15px;border-left-width: 1px;border-left-color: darkgray;border-left-style: solid;
}

7. 使用 QSS 的注意事项

  1. 优先级:更具体的选择器会覆盖更一般的样式
  2. 继承:某些属性会从父控件继承
  3. 动态属性:可以使用 [property="value"] 选择器
    QPushButton[highlight="true"] { color: red; }
    
  4. 应用样式表
    // 单个控件
    widget->setStyleSheet("QPushButton { color: red; }");// 整个应用程序
    QApplication::setStyleSheet("...");
    

QSS 提供了强大的样式定制能力,几乎可以修改 Qt 控件的所有视觉方面。

相关文章:

  • 基于单片机的视力保护仪设计与实现
  • Elecron 相关介绍以及常见的面试问题
  • 18.Excel数据透视表:第1部分创建数据透视表
  • 基于Java的家政服务平台设计与实现(代码+数据库+LW)
  • 分析Docker容器Jvm 堆栈GC信息
  • 【Linux】网络基础2
  • TS类型操作工具汇总
  • switch-case 巧妙使用(case穿透)
  • InfluxDB-数据看板实现流程:从数据采集到可视化展示
  • Spring Boot 自动装配原理详解
  • Cascadeur2025如何无限制导出FBX文件
  • 无偿帮写服务器配置|限时30天|名额100
  • 聊聊JetCache的CachePenetrationProtect
  • Xianyu AutoAgent,AI闲鱼客服机器人
  • 深度学习的相关术语介绍(杂乱版-想到啥介绍啥)
  • uniapp-商城-56-后台 新增商品(弹窗属性继续分析)
  • UDP--DDR--SFP,FPGA实现之指令监测模块实现
  • 专题二:二叉树的深度搜素(二叉树的所有路径)重点理解回溯算法的”恢复现场“
  • 在VSCode中接入DeepSeek的指南
  • 携固态电池、新形态钢壳叠片电池等产品 豪鹏科技将亮相CIBF 2025
  • 山西临汾哪吒主题景区回应雕塑被指抄袭:造型由第三方公司设计
  • 远如《月球背面》,近似你我内心
  • 美国务卿鲁比奥将前往土耳其参加俄乌会谈
  • 习近平同巴西总统卢拉会谈
  • 专访|韩国世宗研究所中国研究中心主任:李在明若上台将推行均衡外交
  • 王毅集体会见加勒比建交国外长及代表