刚开始我只是用简单的qss语句设置QScrollBar,居然完全不起作用!
代码如下:
m_view = new QListView(this);
m_view->setGeometry(20,25,w-40,h-50);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_view->setFrameShape(QFrame::NoFrame);
// 没有效果
//m_view->setStyleSheet("QScrollArea{background-color:transparent;}"
// "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical{width:0;height:0;}"
// "QScrollBar::handle:vertical{image:url(:/ClassBlackBoard/Resources/ClassBlackBoard/Slider.png)}");
m_view->verticalScrollBar()->setStyleSheet("QScrollBar::handle:vertical{image:url(:/ClassBlackBoard/Resources/ClassBlackBoard/Slider.png)}");// 没有效果
苦思冥想都不知道该怎么办!准备自己重写QListView的时候,在别人指导下,重新写了一下qss代码:
m_view->setStyleSheet("QScrollBar:vertical\
{ \
width:8px; \
background:rgba(0,0,0,0%); \
margin:0px,0px,0px,0px; \
padding-top:9px; \
padding-bottom:9px; \
} \
QScrollBar::handle:vertical \
{ \
width:8px; \
background:rgba(0,0,0,15%); \
border-radius:4px; \
min-height:20; \
} \
QScrollBar::handle:vertical:hover \
{ \
width:8px; \
background:rgba(0,0,0,25%); \
border-radius:4px; \
min-height:20; \
} \
QScrollBar::add-line:vertical \
{ \
height:9px;width:8px; \
border-image:url(:/images/a/3.png); \
subcontrol-position:bottom; \
} \
QScrollBar::sub-line:vertical \
{ \
height:9px;width:8px; \
border-image:url(:/images/a/1.png); \
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,5%); \
border-radius:4px; \
}");
居然有效果了!
然后逐句删掉多余的qss语句,发现原来是没有给定宽高值。。。
最后代码如下:
m_view = new QListView(this);
m_view->setGeometry(20,25,w-40,h-50);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_view->setFrameShape(QFrame::NoFrame);
m_view->setItemDelegate(new ItemDelegate(this));
m_view->setFocusProxy(this);
m_view->setStyleSheet("QScrollBar:vertical\
{\
width:15px;\
background:rgba(0,0,0,0);\
padding-bottom:9px; \
background-position: right;\
} \
QScrollBar::handle:vertical \
{ \
width:15px; \
border-image:url(:/ClassBlackBoard/Resources/ClassBlackBoard/Slider.png); \
} \
QScrollBar::add-line:vertical\
{\
height:0px;width:0px;\
subcontrol-position:bottom;\
}\
QScrollBar::sub-line:vertical\
{\
height:0px;width:0px;\
subcontrol-position:top;\
}");