我想突出显示滚动条的某些部分,例如在谷歌浏览器中突出显示搜索。突出显示滚动条
见http://dl.dropbox.com/u/17404614/scrollbar.JPG
是否有可能使用Qt来实现这一目标?
感谢
我想突出显示滚动条的某些部分,例如在谷歌浏览器中突出显示搜索。突出显示滚动条
见http://dl.dropbox.com/u/17404614/scrollbar.JPG
是否有可能使用Qt来实现这一目标?
感谢
您可以根据QScrollBar
创建自己的ScrollBar
类,并重新实现virtual void paintEvent (QPaintEvent *)
方法。不要忘记调用原来paintEvent
手柄,如果你看什么你的亮点下的实际滚动条...
应该是这样的:
void SrollBar::paintEvent(QPaintEvent * event)
{
QScrollBar::paintEvent(event); //Qt scroll bar is rendered now
QPainter p(this);
//here do what ever you want like painting rectangles with alpha = 0.5 ...
}
您可以通过继承QScrollBar
和更换做到这一点小部件的滚动条与您自己的,然后重写paintEvent
以调用超类paintEvent
,然后在顶部绘制突出显示。
paintEvent
覆盖中的重要内容是将绘图限制和缩放到滚动条凹槽,同时避免在滑块顶部绘图。您可以通过剪切到凹槽矩形减去滑块矩形(如initStyleOption(QStyleOptionSlider *)
计算)来做到这一点。
此代码是在Python,但它应该是相当简单的翻译到C++:
_ANNOTATION_SCROLLBAR_COLOUR = "gold"
document_height = 100
annotations = [(10, 20), (50, 51), (82, 85)]
class AnnotatedScrollBar(QtGui.QScrollBar):
def paintEvent(self, event):
super(AnnotatedScrollBar, self).paintEvent(event)
p = QtGui.QPainter(self)
opt = QtGui.QStyleOptionSlider()
self.initStyleOption(opt)
gr = self.style().subControlRect(QtGui.QStyle.CC_ScrollBar, opt,
QtGui.QStyle.SC_ScrollBarGroove, self)
sr = self.style().subControlRect(QtGui.QStyle.CC_ScrollBar, opt,
QtGui.QStyle.SC_ScrollBarSlider, self)
p.setClipRegion(QtGui.QRegion(gr) - QtGui.QRegion(sr),
QtCore.Qt.IntersectClip)
x, y, w, h = gr.getRect()
c = QtGui.QColor(_ANNOTATION_SCROLLBAR_COLOUR)
p.setBrush(c)
c.setAlphaF(0.3)
p.setPen(QtGui.QPen(c, 2.0))
yscale = 1.0/document_height
p.drawRects([QtCore.QRect(x, y + h * start * yscale - 0.5,
w, h * (end - start) * yscale + 1)
for start, end in annotations])