Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt horizontalSlider send float values

Tags:

python

qt

slider

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.......

like image 454
codeKiller Avatar asked Dec 17 '13 11:12

codeKiller


2 Answers

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.

like image 111
Pavel Strakhov Avatar answered Oct 24 '22 02:10

Pavel Strakhov


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)
like image 5
Aleksandar Avatar answered Oct 24 '22 02:10

Aleksandar