我想突出显示滚动条的某些部分,例如在谷歌浏览器中突出显示搜索。
参见 http://dl.dropbox.com/u/17404614/scrollbar.JPG
是否可以使用 Qt 来实现这一点?
谢谢
最佳答案
您可以通过子类化 QScrollBar
并用您自己的滚动条替换小部件的滚动条,然后覆盖 paintEvent
以调用父类(super class) paintEvent
和然后在上面画高光。
paintEvent
重写中的重要事项是将绘图限制和缩放到滚动条凹槽,同时避免在 slider 顶部绘图。您可以通过剪裁到凹槽矩形减去 initStyleOption(QStyleOptionSlider *)
计算的 slider 矩形来执行此操作。
此代码是用 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])
关于qt - 突出显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6432656/