I want to send a float value "step" with a qt horizontalSlider, I am trying this, and it is not working:
horizontalSlider.setRange(0,25)
horizontalSlider.setSingleStep(horizontalSlider.maximum()/100.0)
horizontalSlider.valueChanged.connect(self.valueHandler)
Then I am getting the value here:
def valueHandler(self,value):
print value
But, however, the output Im getting is 1,2,3,4,5,6,7,8.......
Qt silders can have only integer values. You need to change your measuring unit so that integer precision would be enough. For example, usually 100 positions of slider can be considered enough. So you need to set (0, 100) slider range and scale your float values so that minimal value is converted to 0 and maximal value is converted to 100 (i.e. convert your float values to percents).
You can create a subclass of Qt slider and implement the described logic internally if you want to keep your main code clean.
Since you want single step to be 25/100 = 0.25, this is how you should do it:
horizontalSlider.setRange(0,100)
horizontalSlider.setSingleStep(1)
horizontalSlider.valueChanged.connect(self.valueHandler)
def valueHandler(self,value):
scaledValue = float(value)/4 #type of "value" is int so you need to convert it to float in order to get float type for "scaledValue"
print scaledValue , type(scaledValue)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With