一 、换肤
void CWidgetQSS::on_btnQSSLoad_clicked()
{
//加载样式表
QFile file("../QSS/widgetqss.qss");
if (file.open(QFile::ReadOnly))
{
QString qss = QLatin1String(file.readAll());
//必需要先设置空,否则设置不生效,原因我也不知道(QT5.13.1)
this->setStyleSheet("");
this->setStyleSheet(qss);
file.close();
}
}
二、九宫格
1、九宫格规则如下:
1)将一张图分割成9块
2)四个角(1,3,7,9)在缩放的时候是保持大小不变
3)图块2,8仅当宽度变化时缩放宽度。
4)图块4,6仅当高度变化时缩放高度。
5)图块5当图片大小发生变化,宽度和高度都进行缩放。
border-image: url(xxx.png) 顶 右 底 下 no-repeat no-repeat no-repeat no-repeat;
2、示例
1)QLineEdit
原图:
//QSS文件
QLineEdit{
border-width:1px 1px 1px 1px;
border-image: url(xxx.png) 1 1 1 1 no-repeat no-repeat no-repeat no-repeat;
}
效果图:
2)QPushButton
原图:
//QSS文件
QPushButton{
width:60px;
height:60px;
border-width:30px 30px 30px 30px;
border-image: url(C:/Users/aa/Desktop/9.png) 30 30 30 30 no-repeat no-repeat n0-repeat no-repeat;
}
效果图:
三、表格 QTableWidget
//水平表头首圆弧
QHeaderView::section:horizontal:first{
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
}
//水平表头尾圆弧
QHeaderView::section:horizontal:last{
border-top-right-radius: 8px;
border-bottom-right-radius: 8px;
}
//水平表头背景
QHeaderView::section:horizontal{
background-color: red;
}