1

I am making a gui with multiple QSlider in a scrollarea, for some of my sliders i need an interval of 10 and not 1.

When i open the gui the ticks are correct and have a jump of 10, but when i return the current value i can still get values that are between the ticks, for example:

slider = QtGui.QSlider(QtCore.Qt.Horizontal)
slider.setMaximum(90)
slider.setMinimum(10)
slider.setTickInterval(10)
slider.setTickPosition(QtGui.QSlider.TicksBelow)
slider.valueChanged.connect(self.slider_value_change)

The function is just:

def slider_value_change(self,value):
    print(value)

This code will return 10,11,12,13... instead of 10,20...

Is there a way to keep the value so it will only return the tick values? Also is there a way to add a lable to the tick with its value?

Drza loren
  • 103
  • 1
  • 3
  • 10

2 Answers2

2

The simple way to solve this is to think in terms of the number of slider positions, rather than their specific values.

For a value range of 10-90 with an interval of 10, there are only 9 possible positions. So the slider should be configured like this:

slider = QtGui.QSlider(QtCore.Qt.Horizontal)
slider.setMinimum(1)
slider.setMaximum(9)
slider.setTickInterval(1)
slider.setSingleStep(1) # arrow-key step-size
slider.setPageStep(1) # mouse-wheel/page-key step-size
slider.setTickPosition(QtGui.QSlider.TicksBelow)
slider.valueChanged.connect(self.slider_value_change)

Now all you need to do is multiply by 10 to get the required value:

def slider_value_change(self, value):
    print(value * 10)

The basic idea here, is that setting an interval of 1 eliminates the possibility of intervening values - so it is only possible to drag the slider through a fixed set of positions.


On the question of tick-mark labels, see:


PS: If you want mouse-clicks to jump straight to a slider position, see:

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

Actually, no, there is no trivial solution to this so your best bet is to sanitize the value in self.slider_value_change.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • 1
    Is it also true for the labels? do i need to add them manualy by position or is there a simpler way? – Drza loren Nov 26 '17 at 10:24
  • You can update the slider value to the correct one whenever it changes. Just make sure you don't run into a feedback loop. – ypnos Nov 26 '17 at 10:40
  • i meant the labels for each tick(10,20,30...), is there a way to add them when creating the slider? – Drza loren Nov 26 '17 at 10:50
  • 1
    See https://stackoverflow.com/questions/27661877/qt-slider-widget-with-tick-text-labels – ypnos Nov 26 '17 at 11:41