使用float for QSlider

时间:2017-03-15 20:46:44

标签: python pyqt pyqt4 pyqt5 qslider

我有一个QLineEdit和一个QSlider,它可以互相交互。

EG。如果我在QLineEdit中设置了一个值,滑块将会更新,或者如果我滑动滑块,它将更新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))

无论如何,我可以使用float代替int值吗?

3 个答案:

答案 0 :(得分:1)

经过多次调查后,这对我有用:

# 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)

答案 1 :(得分:0)

@ dissidia的答案很好。但是如果你的应用程序中有很多滑块,或者你需要几个不同的比例因子,那么将QSlider子类化为自己的QDoubleSlider是值得的。

下面的课程基于其他人的工作,但是如果你链接到QLineEdit或QDoubleSpinBox,你还有一个额外的功能:valueChanged的一个新信号叫做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))

答案 2 :(得分:0)

我修改了bfris的答案,以使用户无法在步骤之间跟踪滑块,如下所示。该类使用另一种方法来实现浮点数的使用,这意味着不需要指定小数位数。

FooBar: Type: "AWS::DynamoDB::Table" Properties: BillingMode: PAY_PER_REQUEST TableName: FooBarTable AttributeDefinitions: - AttributeName: "pk" AttributeType: "S" - AttributeName: "sort" AttributeType: "S" - AttributeName: "runningFiles" AttributeType: "N" KeySchema: - AttributeName: "pk" KeyType: "HASH" - AttributeName: "sort" KeyType: "RANGE" LocalSecondaryIndexes: - IndexName: RunningJobsPerTenant Projection: ProjectionType: INCLUDE NonKeyAttributes: - "isTenant" KeySchema: - AttributeName: "pk" KeyType: "HASH" - AttributeName: "runningFiles" KeyType: "RANGE" 方法等效于组合SetIntervalsetTickInterval方法,但是也可以使滑块停止在刻度值之间。

该类还允许设置和读取在滑块上选择的点的索引。

setSingleStep