tags:

views:

14

answers:

1

Hi all,

I'm wanting to implement a scroll bar that snaps to particular values (like windows can snap to the edge of a screen). The idea is that as I drag the scrollbar down it snaps the bar to values as it approaches them.

My scenario is displaying 3 chapters of text. I would like to be able to snap to the beginning or end of a chapter. Of course, to go to the start of the first chapter the scrollbar one can just scroll to the top and likewise with the end of the third chapter. So I'd like to draw two lines on the scrollbar to represent the start of the second and third chapters and then have the top and bottom of the scrollbar snap to those lines. So I'm really actually wanting to use this within a QTextBrowser but I can control a QTextBrowser with a "QSnapScrollBar" I just don't really know where to start.

Any help would be greatly appreciated.

A: 

You could roll your own custom scrollbar class that inherits from QScrollBar or perhaps QAbstractSlider and override the valueChanged(int) signal. Some basic pseudocode:

if currentValue is within 5 of snapvalue1:
    set scrollbar value to snapvalue1
elsif currentValue is within 5 of snapvalue2:
    set scrollbar value to snapvalue2
...

update/redraw widget

Though I have a feeling that this might seem jerky to the end user.

You could also investigate the pageStep property (which controls how far it will scroll when using the Page Up/Down keys), maybe that could be used to serve your purposes

swanson
overriding signals wont work but thanks - look like I'll do this on mouseMoveEvent - I'll say when I've got a more complete solution
j3frea
looks like I will have to override mouseMoveEvent but I'll accept your solution for pointing me in the right direction - thanks
j3frea