样式表
1.样式表
盒子模型
2.选择器
选择器类型
QPushButton[down = "false"]{color:yellow;background:red}
QLabel[text = "TextLabel"]{color:red}
同时在UI和文件代码里设置样式表会冲突,只会生效ui里设置的
伪状态选择器Pseudo-State
QPushButton:hover{color:red}
3.控件示例
QLabel#label{
font-family: "微软雅黑";
font-size: 20px;
font-style: italic;
color: rgb(239,204,180);
border-image:url("C:/Users/PVer/Pictures/Resource/派蒙.jpeg")
}
QPushButton{
/*上右下左设置padding间距*/
padding:20px 0px 0px 50px;
border: 3px solid black;
border-radius: 10px;
background-color:red;
}
QLineEdit{
border-top:2px solid yellow;
border-right:2px dotted black;
border-top-left-radius:10px;
border-left:3px solid black;
}
QLabel#label_2{
background-image:url("C:/Users/PVer/Pictures/Resource/xiaoku.png");
background-repeat:no-repeat;
background-position:left top;
}
QLabel#label_2:hover{
background-color:green;
}
QPushButton:pressed{
background-color:orange;
}
QCheckBox::indicator{
background-color:grey;
border-radous:5px;
}
QCheckBox::indicator:checked{
background-color:black;
}
QCheckBox::indicator:!checked:hover{
background-color:green
}
4
.细节、注意事项
继承自QWidget的类,设置qss样式表没有效果,需要重写paintEvent
void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.initFrom(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
而且必须使用Q_OBJECT宏