Use float for QSlider

主宰稳场 提交于 2020-01-23 03:29:25

问题


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 across, it will updates the value in QLineEdit

# If user change value on the slider
self.timer_slider.valueChanged.connect(self.set_value)
# If user sets a value in the text box instead
self.timer_value.textChanged.connect(self.set_slider)

def set_slider(self, value):
    self.timer_slider.setValue(int(value))

def set_value(self, value):
    self.timer_value.setText(str(value))

Is there anyway that I can use float instead of int values?


回答1:


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)



回答2:


@dissidia's answer is good. But if you have a lot of sliders in your app or if you need several different scale factors, it pays to subclass QSlider to make your own QDoubleSlider.

The class below is based on the work of others, but has an extra feature you'll want if you link to a QLineEdit or QDoubleSpinBox: a new signal for valueChanged called doubleValueChanged.

class DoubleSlider(QSlider):

    # create our our signal that we can connect to if necessary
    doubleValueChanged = pyqtSignal(float)

    def __init__(self, decimals=3, *args, **kargs):
        super(DoubleSlider, self).__init__( *args, **kargs)
        self._multi = 10 ** decimals

        self.valueChanged.connect(self.emitDoubleValueChanged)

    def emitDoubleValueChanged(self):
        value = float(super(DoubleSlider, self).value())/self._multi
        self.doubleValueChanged.emit(value)

    def value(self):
        return float(super(DoubleSlider, self).value()) / self._multi

    def setMinimum(self, value):
        return super(DoubleSlider, self).setMinimum(value * self._multi)

    def setMaximum(self, value):
        return super(DoubleSlider, self).setMaximum(value * self._multi)

    def setSingleStep(self, value):
        return super(DoubleSlider, self).setSingleStep(value * self._multi)

    def singleStep(self):
        return float(super(DoubleSlider, self).singleStep()) / self._multi

    def setValue(self, value):
        super(DoubleSlider, self).setValue(int(value * self._multi))


来源:https://stackoverflow.com/questions/42820380/use-float-for-qslider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!