QScrollBar分为sub-line、add-line、add-page、sub-page、up-arrow、down-arrow和handle几个部分
/*垂直滑块整体*/
QScrollBar:vertical{
background:#FFFFFF; /*背景色*/
padding-top:20px; /*上预留位置(放置向上箭头)*/
padding-bottom:20px; /*下预留位置(放置向下箭头)*/
padding-left:3px; /*左预留位置*/
padding-right:3px; /*右预留位置*/
border-left:1px solid #d7d7d7; /*左分割线*/
margin:0px 0px 0px 0px;
}
/*滑块样式*/
QScrollBar::handle:vertical{
background:#dbdbdb; /*滑块颜色*/
border-radius:6px; /*边角圆润*/
min-height:80px; /*滑块最小高度*/
margin-left:5px; /*滑块与滑轨左边的距离*/
margin-right:5px; /*滑块与滑轨右边的距离*/
}
/*鼠标触及滑块样式*/
QScrollBar::handle:vertical:hover{
background:#d0d0d0; /*滑块颜色*/
}
/*鼠标触及滚动条样式*/
QScrollBar::vertical:hover{
background:#d0d0d0; /*滑块颜色*/
}
/*鼠标触及滑块样式*/
QScrollBar::handle:vertical:pressed{
background:#d0d0d0; /*滑块颜色*/
}
/*鼠标触及滚动条样式*/
QScrollBar::vertical:pressed{
background:#d0d0d0; /*滑块颜色*/
}
/*向下箭头样式*/
QScrollBar::add-line:vertical{
/*background:url(/images/down.png) center no-repeat;*/
subcontrol-position:bottom;
}
/*向上箭头样式 */
QScrollBar::sub-line:vertical{
/*background:url(:/images/up.png) center no-repeat;}*/
subcontrol-position:top;
}
/*当鼠标放到下箭头上的时候*/
QScrollBar::add-line:vertical:hover{
height:9px;
width:8px;
/*border-image:url(:/images/a/4.png);*/
subcontrol-position:bottom;
}
/*当鼠标放到下箭头上的时候*/
QScrollBar::sub-line:vertical:hover{
height:9px;
width:8px;
/*border-image:url(:/images/a/2.png);*/
subcontrol-position:top;
}
/*当滚动条滚动的时候,上面的部分和下面的部分*/
QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{
background:rgba(0,0,0,10%);
border-radius:4px;
}
QScrollBar::up-arrow:vertical{
subcontrol-origin: margin;
height:16px;
border:0 0 0 0;
border-image: url(:/DirectionArrow/image/DisplayImage/up-row.png);
}
QScrollBar::down-arrow:vertical{
subcontrol-origin: margin;
height:16px;
border-image:url(:/DirectionArrow/image/DisplayImage/down-row.png);
}
调用QSS文件
def load_style_sheet(filename, widget):
with open(filename, 'r', encoding='UTF-8') as f:
style_sheet = f.read()
widget.setStyleSheet(style_sheet)
去掉上下两个箭头
/*向下箭头样式*/
QScrollBar::add-line:vertical{
background:none;
}
/*向上箭头样式 */
QScrollBar::sub-line:vertical{
background:none;
}
/*当滚动条滚动的时候,上面的部分和下面的部分*/
QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{
background:none;
}
————————————————
版权声明:本文为CSDN博主「初学小白Lu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43956958/article/details/121666680
以下为原创部分:
//竖滚动条可滚动部分的样式
QScrollBar:vertical{
border: 1px solid #0068D5;
background-color: rgba(0, 104, 213, 76);
width: 14px; //滚动条整体宽度
margin: 13px 0px 13px 0px; //13px是上下留给箭头按钮的位置
}
//滑块样式
QScrollBar::handle:vertical{
background: #0068D5;
min-height: 20px;
border-radius:1px;
margin:1px 1px 1px 1px;//上下左右留出1px,与边界不重合
}
//鼠标落在滑块上的滑块背景色
QScrollBar::handle:vertical:hover{
background: #2971e2;
}
//滑块按下样式
QScrollBar::handle:vertical:pressed{
background-color: rgba(125, 250, 250, 128);
}
//下方箭头按钮的位置大小边框样式
QScrollBar::add-line:vertical{
border: 1px solid #0068D5;
height: 12px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}
//上方箭头按钮的位置大小边框样式
QScrollBar::sub-line:vertical{
border: 1px solid #0068D5;
height: 12px;
subcontrol-position: top;
subcontrol-origin: margin;
}
//向上箭头样式
QScrollBar:up-arrow:vertical{
width: 12px;
height: 12px;
border-image: url(:/img/blue1/scroll_arrow_up.png);
}
//向下箭头样式
QScrollBar::down-arrow:vertical{
width: 12px;
height: 12px;
border-image: url(:/img/blue1/scroll_arrow_down.png);
}
QScrollBar:up-arrow:vertical:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_upH.png);
margin: 1px 1px 1px 1px;
}
QScrollBar::down-arrow:vertical:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_downH.png);
margin: 1px 1px 1px 1px;
}
QScrollBar:up-arrow:vertical:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_upS.png);
margin: 1px 1px 1px 1px;
}
QScrollBar::down-arrow:vertical:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_downS.png);
margin: 1px 1px 1px 1px;
}
//滑块上方、下方样式
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical{
background: rgba(0, 104, 213, 76);
}
//滑块上方、下方鼠标滑过样式
QScrollBar::sub-page:vertical:hover{
background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(0, 104, 213, 128), stop:1 rgba(0, 104, 213, 68));
}
QScrollBar::add-page:vertical:hover{
background: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(0, 104, 213, 128), stop:1 rgba(0, 104, 213, 68));
}
//横滚动条参照竖滚动条样式说明
QScrollBar:horizontal{
border: 1px solid #0068D5;
background-color: rgba(0, 104, 213, 76);
height: 14px; //注意横滚动条设置的是高度
margin: 0px 13px 0px 13px;//左右留出13px箭头按钮位置
}
QScrollBar::handle:horizontal{
background: #0068D5;
min-width: 20px;
border-radius:1px;
margin:1px 1px 1px 1px;
}
QScrollBar::handle:horizontal:hover{
background: #2971e2;
}
QScrollBar::handle:horizontal:pressed{
background-color: rgba(125, 250, 250, 128);
}
QScrollBar::add-line:horizontal{
border: 1px solid #0068D5;
width: 12px;
subcontrol-position: right;
subcontrol-origin: margin;
}
QScrollBar::sub-line:horizontal{
border: 1px solid #0068D5;
width: 12px;
subcontrol-position: left;
subcontrol-origin: margin;
}
QScrollBar:left-arrow:horizontal{
width: 12px;
height: 12px;
border-image: url(:/img/blue1/scroll_arrow_left.png);
}
QScrollBar::right-arrow:horizontal{
width: 12px;
height: 12px;
border-image: url(:/img/blue1/scroll_arrow_right.png);
}
QScrollBar:left-arrow:horizontal:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_leftH.png);
margin: 1px 1px 1px 1px;
}
QScrollBar::right-arrow:horizontal:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_rightH.png);
margin: 1px 1px 1px 1px;
}
QScrollBar:left-arrow:horizontal:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_leftS.png);
margin: 1px 1px 1px 1px;
}
QScrollBar::right-arrow:horizontal:hover{
width: 10px;
height: 10px;
border-image: url(:/img/blue1/scroll_arrow_rightS.png);
margin: 1px 1px 1px 1px;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal{
background: rgba(0, 104, 213, 76);
}
QScrollBar::sub-page:horizontal:hover{
background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 104, 213, 128), stop:1 rgba(0, 104, 213, 0));
}
QScrollBar::add-page:horizontal:hover{
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:0, stop:0 rgba(0, 104, 213, 128), stop:1 rgba(0, 104, 213, 0));
}