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

【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 控件的所有视觉方面。

http://www.xdnf.cn/news/5774.html

相关文章:

  • [250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
  • MapReduce 模型
  • AI 模型训练轻量化技术在军事领域的实战应用与技术解析
  • Excelize 开源基础库发布 2.9.1 版本更新
  • ThingsBoard使用Cassandra部署时性能优化
  • c++进阶——哈希表的实现
  • Linux进程信号处理(26)
  • Maven 动态插件配置:Profile的灵活集成实践
  • 小白成长之路-vim编辑
  • 阿克曼-幻宇机器人系列教程2- 机器人交互实践(Topic)
  • 快速上手Linux nfs网络文件系统
  • 仿正点原子驱动BMP280气压传感器实例
  • 3335. 字符串转换后的长度 I
  • Babylon.js学习之路《四、Babylon.js 中的相机(Camera)与视角控制》
  • MySQL基本查询
  • git经验
  • Electron-Vue3、Electron-React、Electron-Angular打造舆情监控系统项目
  • SimScape物理建模实例1--单质量-弹簧-阻尼系统
  • maxtext开源程序是一个简单、高性能和可扩展的 Jax LLM!
  • rsync
  • 2024年北理工Python123第六章测验题整理
  • 2094. 找出 3 位偶数
  • 稠密连接网络(DensoNet)
  • OFCMS代码审计-freemaker注入sql注入xxexss文件上传
  • Qt元对象系统总结
  • .NET10 - 尝试一下Open Api的一些新特性
  • 吴恩达机器学习笔记:监督学习
  • 【Python】Python常用控制结构详解:条件判断、遍历与循环控制
  • 015枚举之滑动窗口——算法备赛
  • 纽约时报发稿刊登案例:海外新闻媒体宣传如何赢得美国决策者