Use float for QSlider

前端 未结 3 1043
轻奢々
轻奢々 2021-01-07 01:05

I have a QLineEdit and a QSlider in which it interacts with each other.

Eg. If I set a value in the QLineEdit, the slider will be updated, or if I slide the slider a

3条回答
  •  耶瑟儿~
    2021-01-07 01:30

    After much findings, this works for me:

    # Connection Signals
    
    # When user tweaks using the slider
    self.slider.valueChanged[int].connect(self.update_spinbox)
    # When user modify via the spinbox
    self.spinbox_value.editingFinished.connect(self.update_slider)
    
    
    # Functions for each modication made towards slider and spinbox
    def update_slider(self):
        # spinbox_value uses float/ doubles type
        # '*100' is used to convert it into integer as QSlider
        # only register integer type
        spinbox_value = self.spinbox_value.value() * 100
        self.slider.setSliderPosition(spinbox_value)
    
    def update_spinbox(self, value):
        # QSlider only uses integer type
        # Need to convert the value from integer into float
        # and divides it by 100
        self.spinbox_value.setValue(float(value) / 100)
    

提交回复
热议问题