QT样式表总结
添加方式
1.直接在Qt设计器中选中控件选择改变样式
2.在代码中使用setstylesheet函数添加样式
3.创建qss文件添加样式
选择器
*通用选择器 (e.g: )
匹配所有的控件
类型选择器 (e.g: QPushButton)
```cpp
QPushBuuton
{
background-color: rgb(0, 255, 255);
}
属性选择器 (e.g: QPushButton[flat=“false”])
QPushButton
{
color:rgb(10,0,0);
border:none;
}
QPushButton[flat = "true"]
{
background-color:rgb(10,10,10);
}
QPushButton[flat = "false"]
{
background-color:rgb(255,255,255);
}
代码也可以对控件设置属性:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->btn_3->setProperty("name","xiaohua");
ui->btn_4->setProperty("name","lisheng");
this->setStyleSheet("QPushButton{color:rgb(255,255,255);}"
"QPushButton[name = 'xiaohua']{background:rgb(0,0,255);}"
"QPushButton[name = \"lisheng\"]{background:rgb(255,0,0);}");
//设置属性的值用双引号或者单引号都可以
类选择器 (e.g: .QPushButton)
.QPushBuuton
{
background-color: rgb(0, 10, 10);
border:none;
}
匹配QPushButton的实例,但不匹配其子类的实例,这个可以说与类型选择器是一对,这种选择器只会匹配该类的所有对象, 而不会匹配其派生类的对象。
ID选择器 (e.g: QPushButton#okButton)
QPushButton#okButton
{
background-color:rgb(50,50,50);//声明
}
后代选择器 (e.g: QDialog QPushButton)
QFrame QPushBuuton
{
background-color: rgb(0, 10, 10);
border:none;
}
匹配所有QPushButton实例,它们是QFrame的后代(子代,孙子代等)
子代选择器 (e.g: QDialog > QPushButton)
QFrame > QPushBuuton
{
background-color: rgb(10, 25, 25);
border:none;
}
匹配所有QPushButton实例,它们是QFrame的直接子代
伪类
伪状态对类型选择器或类选择器指定的所有控件设置它在指定状态时的样式,伪状态以冒号(:)作为分隔 紧跟着选择器,状态伪类很多,上图是从官方截取的,当鼠标滑过或者按下去,控件本身就会发送变化,伪状态就是运用到了这里。
声明
声明就是{}号里面的内容,例如:
background-image:url(); 背景图片,不会自适应
color:rgb(14, 18, 60);文字颜色
background:rgb(20, 10, 30); 背景颜色
border-image:url(); 背景图片,会自适应控件大小
border-color:rgba(0,0,0,255); 边框颜色
border-radius:10px;圆角大小
border-style:none; 边框风格
font:10px “宋体”;字体大小/字体
font-weight: bold;字体粗细
text-decoration:line-through;字体修饰