python – Qt horizo​​ntalSlider发送浮点值

我想用qt horizo​​ntalSlider发送一个浮点值“step”,我正在尝试这个,它不能正常工作:

    horizontalSlider.setRange(0,25)
    horizontalSlider.setSingleStep(horizontalSlider.maximum()/100.0)
    horizontalSlider.valueChanged.connect(self.valueHandler)

然后我在这里获得价值:

    def valueHandler(self,value):    
        print value

但是,我得到的输出是1,2,3,4,5,6,7,8 …….

最佳答案 由于您希望单步为25/100 = 0.25,因此您应该这样做:

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)
点赞