Qt笔记-QScrollBar- qss

来源:互联网 发布:加拿大生物专业知乎 编辑:程序博客网 时间:2024/05/29 08:04

//QSS
{
strStyle.append(” QScrollBar:horizontal{ “);
strStyle.append(” max-height:21px; “);
strStyle.append(” min-height:21px; “);
strStyle.append(” border-top:1px solid rgba(0,0,0,50); “);
strStyle.append(” border-bottom:1px solid rgba(0,0,0,50); “);
strStyle.append(” margin:0px,0px,0px,0px; “);
strStyle.append(” padding-left:27px; “);
strStyle.append(” padding-right:27px; “);
strStyle.append(” background-color: rgb(255, 255, 255); “);
strStyle.append(” } “);
strStyle.append(” QScrollBar::handle:horizontal{ “);
strStyle.append(” height:21px; “);
strStyle.append(” min-width:120; “);
strStyle.append(” background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 255, 255, 255),stop:0.048 rgba(255,255, 255, 255), stop:0.05 rgba(248,248, 248, 255), stop:1 rgba(238, 238, 238, 255),); “);
strStyle.append(” border-left:1px solid rgba(0,0,0,50); “);
strStyle.append(” border-right:1px solid rgba(0,0,0,50); “);
strStyle.append(” } “);
strStyle.append(” QScrollBar::sub-line:horizontal{ “);
strStyle.append(” margin:0px,0px,0px,0px; “);
strStyle.append(” width:27px; “);
strStyle.append(” subcontrol-position:left bottom; “);
strStyle.append(” background: url(:/image/ScrollBarHorLeft.png) no-repeat; “);
strStyle.append(” } “);
strStyle.append(” QScrollBar::add-line:horizontal{ “);
strStyle.append(” margin:0px,0px,0px,0px; width:27px; “);
strStyle.append(” border-left:0px solid rgba(0,0,0,50); “);
strStyle.append(” border-right:0px solid rgba(0,0,0,50); “);
strStyle.append(” border-top:0px solid rgba(0,0,0,50); “);
strStyle.append(” border-bottom:0px solid rgba(0,0,0,50); “);
strStyle.append(” background:url(:/image/ScrollBarHorRight.png) no-repeat; “);
strStyle.append(” subcontrol-position:right bottom; “);
strStyle.append(” } “);
}

//除了设QSS外,还需要做下面的一些设置,以保证效果能生效
//方法1:
//备注:如果出来的效果不行,则需要使用一个QWidget包一下
QString parentStyle = ((QWidget *)p->parent())->styleSheet();
((QWidget *)p->parent())->setStyleSheet(“”);
p->setStyleSheet( p->styleSheet() + strStyle );
((QWidget *)p->parent())->setStyleSheet(parentStyle);

//方法二:
//备注:在没有QLayout下使用,没有问题,如果需要使用,则使用方法1
QObject *oldParent = p->parent();
if ( oldParent->inherits(“QWidget”) )
{
QWidget w;
p->setParent(&w);
p->setStyleSheet( p->styleSheet() + strStyle );
p->setParent((QWidget *) oldParent);
}