Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSlider needs to step with custom step value on mouse slide
kirangps last edited by
I am implementing QSlider class in my project.
Requirement is, on slide move, using mouse, slider position must set to custom value defined.
I have tried slider->setSingleStep() and slider->setTickInterval(), they work only if I move slide with Key Board, they don't seem to have effect on mouse slide.
To give an example:
QSlider *slide1 = new QSlider();
slide1->setSingleStep(10); //has no effect on mouse move
slide1->setTickInterval(10); //has no effect on mouse move
This has effect only when I move slide position with key board; not on mouse. When I try to slide with mouse, step increases with value 1 always. With above code what I am trying to achieve is on mouse slide, value increases with step of 10.
Help me in solving this.
Thanks in advance.
Can't you just force the step by checking on every value change, if the new value is not +/- 10 the old value set it to the step value depending whether you moved it up or down.
Or even better and easier - set the maximum for your slider to maximum/10, this way if your slider has to value from 0 to 100, you set its maximum value to 10, you get only 10 positions even with the mouse, and when you use the value just multiply it back to 10 to get the actual value.
kirangps last edited by
Hi utcenter, thanks for the reply and thanks for the workaround, 2nd method helped me.
But I would like to put forward this, why is Qt Slider doesn't have such property, where I can define my custom step size??
I honestly don't know. The two methods you used are not really intended to serve the purpose you want, setSingleStep is for keyboard arrows, setTickInterval is purely visual, and this feature might not get implemented at all, since QWidgets are no longer on focus in the development pipeline. But it is easy to workaround as you see, so there is really no need of it. Plus you can always extend QSlider and define more advanced behavior according to your needs.
What would be the point (from the slider technical perspective, not the user) to have a range say 0-20 with only possible values 0, 10 and 20?
As utcenter said if you need something like this you make range 0-2 and multiply by 10 if you need to use the actual value somewhere. There's really no trickery to it. I doubt there is any value in complicating QSlider interface if this can be easily achieved with existing properties.
Like it is that hard to extend QSlider or even QAbstractSlider, call it MyStepSlider or something, add a private step int and overload the set to set value/step and the get to return value*step and the set max accordingly ;) Not all that complicated to build it in if necessary
Also, don't forget to mark your thread with [SOLVED] once it is done