1.在设计器中拖动QSlider到界面上,编辑滑块的qss样式,将Qt助手Qt Style Sheets Examples中QSlider样式代码粘贴到输入框:
QScrollBar:horizontal {
border: 2px solid green;
background: cyan;
height: 15px;
margin: 0px 40px 0 0px;
}
QScrollBar::handle:horizontal {
background: gray;
min-width: 20px;
}
2.要对QSlider的滑槽groove和把柄handle进行修改,对上面代码进行更改将滑块的槽和把柄换成图片,代码如下所示:
QSlider::groove:horizontal {
/*border: 1px solid #999999;*/
height:28px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
/*background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);*/
/*background-image: url(:/images/sliderbackground.png);*//*注意:此属性会挤压handle图片导致高度不能调整*/
image: url(:/images/sliderbackground.png);
margin: 2px 0;
}
QSlider::handle:horizontal {
/* background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);*/
image: url(:/images/sliderhandle.png);
/*border: 1px solid #5c5c5c;*/
width: 28px;
margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
/*border-radius: 3px;*/
}
3.千万注意,QSlider::groove:horizontal要替换图片,不能用background-image属性,只能用image属性,不然会挤压QSlider::handle:horizontal的图片导致高度不能调整(宽度能调整)。
4.参考文献 《Qt助手·Qt Style Sheets Examples》