Drza loren
Drza loren

Reputation: 103

Python PyQt4 QSlider Interval bigger than 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?

Upvotes: 1

Views: 2423

Answers (2)

ekhumoro
ekhumoro

Reputation: 120608

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:

Upvotes: 4

ypnos
ypnos

Reputation: 52337

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

Upvotes: 1

Related Questions