Qt &QSS

时间:2021-08-09 16:49:40

Today task:
解决了qt中的一些控件无法使用qss的问题(如QProgressDialog 调节chunk的颜色,QMessageBox按钮的颜色问题)
1,对于QMessageBox,可以单独设置Button的qss
如:style += QPushButton(color: rgb(...), backround-color: rgb(...); height: 12px };
可是,有些时候,字体颜色无法使用qss所规定的
那么可以使用一下方法来使其生效。

 QObjectList objectChildList = msgBox->children();
for(int i=; i<objectChildList.size(); i++)
{ QObject *pObj= objChildList.at(i);
if (pObj->inherits("QLabel"))
{
QWidget *pWidget = (QWidget*)pObj;
pWidget->setStyleSheet("color: rgb(255,255,255)");
}
}

然后设置其sytle。

对于QProgressDialog

     style +="QProgressDialog{ background-color: rgb(108, 110, 108)}";
style +="QPushButton{color: rgb(255,255,255); background-color: rgb(66, 66, 66);height: 14px; width: 100px}";
style +="QPushButton::hover{color: rgb(255,255,255); background-color: rgb(74, 74, 74);height: 14px; width: 100px}"; QObjectList objChildList = qPDlg->children();
for (int i = ; i < objChildList.size(); i++)
{
QObject *pObj= objChildList.at(i);
if (pObj->inherits("QProgressBar"))
{
QWidget *pWidget = (QWidget*)pObj;
QProgressBar *pBWidget = (QProgressBar *)pWidget;
QString sheet;
sheet += "QProgressBar{color: rgb(255,255,255); background-color:rgb(48, 48, 48);border-radius: 2px;}";
sheet += "QProgressBar::chunk{background-color:rgb(176,138,32);}";
pWidget->setStyleSheet(sheet);
pBWidget->setAlignment(Qt::AlignCenter);
}
else if (pObj->inherits("QLabel"))
{
QWidget *pWidget = (QWidget*)pObj;
pWidget->setStyleSheet("color: rgb(255,255,255)");
}
}

3, QTreeView stylesheet

         QString style;
style +="QTreeView::Item{background-color:rgb(66, 66, 66);}";
style +="QTreeView::Item::selected{background-color:rgb(0,54,162);}";
style +="QTreeView::Item{color: rgb(255, 255, 255);}"; style +="QTreeView::branch:has-siblings:!adjoins-item {background:rgb(0,54,162);}";
style +="QTreeView::branch:has-siblings:adjoins-item {background:rgb(0,54,162);}";
style +="QTreeView::branch:!has-children:!has-siblings:adjoins-item {background:rgb(66, 66, 66);}";
style +="QTreeView::branch:has-children:!has-siblings:closed {background:rgb(108, 110, 108);}";
style +="QTreeView::branch:has-children:!has-siblings:closed:selected {background:rgb(0,54,162);}"; style +="QTreeView::branch:open:has-children:!has-siblings:selected {background:rgb(0,54,162);}";
style +="QTreeView::branch:open:has-children:has-siblings:selected {background:rgb(0,54,162);}";//>>>>>>>>>>>> // style +="QTreeView::branch:close:has-children:!has-siblings {background:rgb(108, 110, 108);}";
// style +="QTreeView::branch:close:has-children:!has-siblings:selected {background:rgb(108, 110, 108);}";
style +="QTreeView::branch:close:has-children:has-siblings {background:rgb(66, 66, 66);}";
// style +="QTreeView::branch:close:has-children:has-siblings:selected {background:rgb(0,54,162);}"; style +="QTreeView::branch:has-siblings:!adjoins-item {border-image: url(:/Icons/stylesheet-vline.png) 0;}"; style +="QTreeView::branch:has-siblings:adjoins-item {border-image: url(:/Icons/stylesheet-branch-more.png) 0;}"; style +="QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image: url(:/Icons/stylesheet-branch-end.png) 0 }"; style +="QTreeView::branch:has-children:!has-siblings:closed,QTreeView::branch:closed:has-children:has-siblings {border-image: none;image: url(:/Icons/stylesheet-branch-closed.png);}"; style +="QTreeView::branch:open:has-children:!has-siblings,QTreeView::branch:open:has-children:has-siblings {border-image: none;image: url(:/Icons/stylesheet-branch-open.png);}"; style += "QTreeView{background: rgb(66, 66, 66);}";
style += "QTreeView::HeaderView{background: rgb(66, 66, 66);}"; style += "QHeaderView::section {background-color:rgb(66, 66, 66);color: white; border: 0 px; font-weight: bold;}";
style += "QWidget{background: rgb(66, 66, 66); color: white;}";
setStyleSheet(style);