只需要在全局设置 QApplication 的 StyleSheet,整个程序的所有控件都会参考css文件的效果
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile styleFile("style.css");
if(styleFile.open(QIODevice::ReadOnly)) {
QString styles = styleFile.readAll();
a.setStyleSheet(styles);
styleFile.close();
}
MainWindow w;
w.show();
return a.exec();
}
部分css文件内容
MainWindow { background:rgb(255,0,0);}
QPushButton { background:rgb(0,0,255); border:2px solid #ff0000 }
QPushButton:hover{background-color:rgb(0, 255, 0); border:5px dotted #00ff00}
QPushButton:pressed{background-color:rgb(0, 0, 255); border:10px groove #0000ff}
更多属性可以参考
https://blog.csdn.net/u013968786/article/details/51295744