Custom behavior for QSlider - tips/hints needed
-
Hey mates
I'd like to change behavior of QSlider - to forbid other middle values than step(), ex min = 0, max = 10, step = 2, so the slider may get only values 0, 2, 4, 6, 8, 10.
Unfortunately, dragging slider by mouse, or pointing a point on a slider and holding LMB causes the slider to set any value in range <min, max>. Any hints how I could change that assuming I'd like to preserve as much original mouseXXXEvent() as possible?I don't mind reimplementing these functions, though. -
@MasterBLB
in that cause.A quick and dirty workaround could be:
QSlider *slider(new QSlider); slider->setRange(0,100); slider->show(); QObject::connect(slider, &QSlider::valueChanged, slider, [slider](int value){ const int step = 5; int offset = value % step; if( offset != 0) slider->setValue(value - offset); });
use at one's own risk
-
@MasterBLB
easiest would be- set the range of the slider to <0, step-count>
- whenever you need to process the value calculate it:
min + value * stepSize
-
Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?
-
@MasterBLB
Maybe I'm completely off here, but QSlider has 2 Settings/Properties that handle the step rangesingleStep
andpageStep
can't you simply set both to your desiered setp-value?
-
That was the 1st I've tried (real used values are range <0, 100>, step 5) but it's still possible to set slider value to say 67 by clicking LMB on slider handle, and move it via mouse move.
And I'd like the handle to stay in place while being dragged via mouse, and be moved only if the new value would be multiplication of step. -
@MasterBLB said in Custom behavior for QSlider - tips/hints needed:
Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?
no.
my suggestion doesn't require any change/subclassing of QSlider. Just how you interpret the returned value -
@MasterBLB
in that cause.A quick and dirty workaround could be:
QSlider *slider(new QSlider); slider->setRange(0,100); slider->show(); QObject::connect(slider, &QSlider::valueChanged, slider, [slider](int value){ const int step = 5; int offset = value % step; if( offset != 0) slider->setValue(value - offset); });
use at one's own risk
-
Thanks @J-Hilk , the last workaround works almost exactly as I'd like it to - however, middle values are still sent via signal valueChanged() :/ though I think I could add logic into receiver to ignore everything which is not sliderValue % sliderStep() = 0
EDIT:
SPLENDID, the combined approach worked! Thank you very much Brother. -
@kshegunov said in Custom behavior for QSlider - tips/hints needed:
That's odd, it may be a bug in the forum software. I can see it for all posts, but it should be available to you too. I'll make inquiries.
Thanks, @kshegunov. The inquiry is at https://forum.qt.io/topic/97183/mark-as-correct-answer-not-appearing-for-some-posts
@MasterBLB, I've marked the solution for you in the meantime.