1 样式表语法
1.1 样式规则
一个样式规则由 选择器和声明 构成.
选择器 指定哪个widget受该规则影响.
声明 指定哪些属性被设置.
例:
QPushButton { color: red } // QPushButton 与 QPushButton 的子类的前景色为红色
QPushButton, QLineEdit, QComboBox { color: red } // 多个选择器由 逗号隔开
QPushButton { color: red; background-color: white } //声明: 属性-值 构成, 多个属性-值 由分号隔开
1.2 选择器类型
注:Qt 样式表支持所有CSS2中定义的选择器 .
通用选择器 | * | 所有widget |
类型选择器 | QPushButton | 所有QPushButton及其子类 |
类选择器 | .QPushButton | 所有QPushButton但不匹配其子类 |
属性选择其 | QPushButton[flat=“false”] | |
ID选择器 | QPushButton#okButton | 对象名称为okButton的QPushButton |
子孙选择器 | QDialog QPushButton | QDialog的所有类型为QPushButton子孙 |
孩子选择器 | QDialog > QPushButton | QDialog的所有类型为QPushButton的孩子, 而不是孙子. |
1.3 子控件
选择器::子控件
例: QComboBox::drop-down { image: url(dropdown.png) }
1.4 伪状态
选择器:伪状态
例: QPushButton:hover { color: white }
伪状态 | 描述 |
---|---|
hover | 鼠标停留在控件 |
pressed |
1.4 Cascading
样式会覆盖祖先的样式,传播给子孙.
1.5 Inheritance
QT中widget不会自动基础父widget的样式