- setStyleSheet基本语法:this->setStyleSheet("color: rgb(0, 255, 0); font-size:20px");
- 一些常用参数:
font: bold; 是否粗体显示
font-size:20px; 来设定字体大小
font-weight:20px; 来设定字体深浅
color:black ;字体颜色
border-image:""; 用来设定边框的背景图片。
border-radius:5px; 用来设定边框的弧度。可以设定圆角的按钮
border-width: 1px; 边框大小
设置背景颜色
background:transparent; 设置背景为透明
color:rgb(241, 70, 62); 设置前景颜色
selection-color:rgb(241, 70, 62); 用来设定选中时候的颜色
重要问题:
- setStyleSheet设置的样式会被所有子类引用:
this->setStyleSheet("background-color: rgb(0, 255, 0)");
解决:限定模块
this->setObjectName("w2"); this->setStyleSheet("#w2{background-color: rgb(0, 255, 0)}");
2.setStyleSheet设置背景图片时,如果所在类没有父类,既是第一层widget时,则不会显示背景图片,替代方案:
QPalette pal = this->palette(); pal.setBrush(QPalette::Background, QBrush(QPixmap("./pic/1.jpg"))); setPalette(pal);
3.相对路径的问题:Windows用相对路径时:"./pic/1.jpg"
查询相对路径所在文件夹:
#include <QDebug> #include<QDir> qDebug() << QDir::currentPath();