首先是添加QSS文件
void addQss(QString filePath)
{
QFile file(filePath);
if( file.open(QFile::ReadOnly)){
qApp->setStyleSheet(qApp->styleSheet()+file.readAll());
}
file.close();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
addQss(":/res/label.qss");
addQss(":/res/ListView.qss");
addQss(":/res/menu.qss");
addQss(":/res/PushButton.qss");
addQss(":/res/Slider.qss");
return a.exec();
}
label.qss ,qss文件名随便命名都行
*
{
color:white;
}
QLabel#state
{
background:rgb(20,20,20,150);
}
QWidget#video
{
border:1px solid rgb(10,10,10,100);
}
QCheckBox:!enabled
{
color:rgb(150,150,150);
background:rgb(60,60,60,100);
}
listview.qss
QListView
{
background:rgb(30,30,30);
border:1px solid rgb(10,10,10,100);
}
QListView:item:selected
{
color:white;
}
QListView:item:hover
{
background:rgb(100,100,100,150);
}
QListView::item:selected
{
background:rgb(10,10,10,150);
}
QListView::item::selected:!active
{
background:rgb(10,10,10,150);
}
QListView::item:selected:active
{
background:rgb(10,10,10,150);
}
menu.qss
QMenu
{
background:rgb(50,50,50);
border:1px solid rgb(0,0,0,50);
}
QMenu::item:selected
{
background-color:rgb(150,150,150,150);
}
QMenu::item:pressed
{
background-color: rgb(100,100,100,100);
}
pushButton.qss
QPushButton
{
background:rgb(100,100,100);
}
QPushButton:hover
{
background:rgb(100,100,100);
}
QPushButton:pressed
{
background:rgb(100,100,100);
}
QPushButton:!enabled
{
color:rgb(150,150,150);
background:rgb(60,60,60,100);
}
slider.qss
QSlider::groove:horizontal
{
border: none;
height: 4px;
background: rgb(150,150,150,150);
}
QSlider::handle:horizontal
{
border: none;
margin: -5px 0px;
width: 14px;
height: 14px;
border-radius: 7px;
background: #6495E0;
}
QSlider::sub-page:horizontal
{
background: #6495E0;
height: 4px;
}
QSlider::add-page:horizontal
{
background: rgb(150,150,150,150);
height: 4px;
}
效果。。
更多QSS自定义移步:https://blog.csdn.net/liang19890820/article/details/51691212
完整工程:https://download.csdn.net/download/qq_17813937/11809247