一、目的 通过一个文件,修改所有页面的样式布局
二、实现过程
1、新建一个TXT文档,后缀修改为qss
QWidget
{
background-color:#C6D7F7;
color:black;//修改字体
}
QPushButton
{/*按钮无任何操做时*/
background-color:#D6C3DD;/*背景色*/
}
QPushButton:hover
{ /*鼠标悬停在按钮上时*/
background-color:white;
}
QPushButton:pressed
{ /*按钮被按下时*/
background-color:#D6C3DD;
}
二、代码调用 窗口的构造函数中直接调用
void MainWin::loadStyleSheet(const QString &styleSheetFile)
{
QFile file(styleSheetFile);
file.open(QFile::ReadOnly);
if (file.isOpen())
{
QString styleSheet = this->styleSheet();
styleSheet += QLatin1String(file.readAll());//读取样式表文件
this->setStyleSheet(styleSheet);//把文件内容传参
file.close();
qApp->setStyleSheet(styleSheet); //这句可以直接刷新背景色
}
else
{
QMessageBox::information(this,"tip","cannot find qss file");
}
}
void MainWin::changeSkin(int colorflag)
{
this->colorSkin=colorflag;
if(colorSkin==0)
{
this->loadStyleSheet("./resource/Mystylesheet.qss");
}
else if(colorSkin==1)
{
this->loadStyleSheet("./resource/nightStyle.qss");
}
}