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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

√沫影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值