Custom behavior for QSlider - tips/hints needed
-
wrote on 30 Nov 2018, 08:20 last edited by
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. -
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
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
-
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
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
-
wrote on 30 Nov 2018, 08:46 last edited by
Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?
-
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
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?
-
wrote on 30 Nov 2018, 09:20 last edited by
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. -
Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?
@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 -
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
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
-
wrote on 1 Dec 2018, 09:59 last edited by MasterBLB 12 Jan 2018, 10:20
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. -
wrote on 1 Dec 2018, 10:22 last edited by
WTF, why I can't set J.Hilk's post as correct answer?? o.O Moderators, please help!
-
Should be able to. What's not working with it? Did you find the button to do so?
-
wrote on 1 Dec 2018, 10:58 last edited by
I'm poining those 3 dots with mouse, but in the menu there is no option "mark this post as correct answer". I have the option for my posts only o.O
-
I'm poining those 3 dots with mouse, but in the menu there is no option "mark this post as correct answer". I have the option for my posts only o.O
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.
-
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.
@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.
6/14