版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010855021/article/details/52177486
1直接采用setStyleSheet方法
不再赘述。类似css
2加载资源文件,用setObjectName设置
A、css文件保留样式,eg:
QPushButton#btLogin {
min-height: 28px;
max-height: 48px;
min-width: 100px;
max-width: 100px;
font-weight: 500;
color: white;
border:1px solid #0C1686;
border-radius: 4px;
background-color: #0C1686;
}
QPushButton#btLogin:hover:!pressed {
background-color: #0F1BAE;
}
QPushButton#btLogin:pressed {
background-color: #090F60;
}
B、设置样式
// 样式
QFile *file;
if (QFile::exists("style.css"))
{
file = new QFile("style.css");
}
else
{
file = new QFile(":/style/style.css");
qDebug()<<"open failed =!";
}
file->open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream stream(file);
stream.setCodec(QTextCodec::codecForName("UTF-8"));
QString style = stream.readAll();
this->setStyleSheet(style); //子窗口去继承
file->close();
file->deleteLater();
pushBtn->setObjectName("btLogin");