注意:在qss文件中注释只能用/* */,不能用双斜杠
设置样式
setStyleSheet(styleSheet().isEmpty() ? “background-color: red” : “”);
不同级别的样式setStyleSheet()
1. QApplication:对整个应用生效
2. QWidget:对当前窗口生效(如果子窗口指定了父窗口,则子窗口使用父窗口的样式)
3. 控件:对当前控件生效
4. 文本样式:setText()时,文本内容可以用HTML修饰
格式
1. 指定类型的控件统一样式
selector1, selector2, selector3 { attribute:value; }
a. selector1:选择器
b. attribute:属性
c. value:属性值
示例:对所有的QPushButton, QLabel生效
QPushButton, QLabel {
color: #FF0000;
}
2. 指定某个控件的样式
a. 控件设置名字: btn->setObjectName("btnName"); //相当于css里的id
b. selector1#name { attribute:value; }
示例:
#btnName {
color: #00FF00;
}
3. 控件状态(控件不同操作显示不同的样式,在其它状态下只需要设置不用的部分)
selector:status { attribute:value; }
示例:
// 正常状态
QPushButton {
color: #FF0000;
border:1px solid gray;
border-radius:3px;
}
// 鼠标放到控件上
QPushButton:hover {
border:2px solid #00FF00;
}
// 鼠标按下
QPushButton:pressed {
border:2px solid #0000FF;
padding-left:3px;
padding-top:3px;
}
4. 控件细节
QCheckBox{
spacing: 5px;
font-size: 15px;
}
/*checkbox样式设置*/
QCheckBox::indicator {
width: 26px;
height: 50px;
}
/*未选中*/
QCheckBox::indicator::unchecked {
image: url(:/ico/checkbox_unchecked.png);
}
/*选中*/
QCheckBox::indicator::checked {
image: url(:/ico/checkbox_checked.png);
}
常用状态
focus // 获取焦点
pressed // 按下
hover // 指向
checked // 选中
unchecked // 不选中
enable // 使用
disenable // 禁用
on // 开启
off // 关闭