HOME/Articles/

DoubleSlider

Article Outline

Example Python program DoubleSlider.py

Modules

  • from PyQt5.QtWidgets import QSlider

Classes

  • class DoubleSlider(QSlider):

Methods

  • def init(self, args, *kwargs):
  • def _value_range(self):
  • def value(self):
  • def setValue(self, value):
  • def setMinimum(self, value):
  • def setMaximum(self, value):
  • def minimum(self):
  • def maximum(self):

Code

Example Python PyQt program :

from PyQt5.QtWidgets import QSlider

class DoubleSlider(QSlider):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.decimals = 5
        self._max_int = 10 ** self.decimals

        super().setMinimum(0)
        super().setMaximum(self._max_int)

        self._min_value = 0.0
        self._max_value = 1.0

    @property
    def _value_range(self):
        return self._max_value - self._min_value

    def value(self):
        return float(super().value()) / self._max_int * self._value_range + self._min_value

    def setValue(self, value):
        super().setValue(int((value - self._min_value) / self._value_range * self._max_int))

    def setMinimum(self, value):
        if value > self._max_value:
            raise ValueError("Minimum limit cannot be higher than maximum")

        self._min_value = value
        self.setValue(self.value())

    def setMaximum(self, value):
        if value < self._min_value:
            raise ValueError("Minimum limit cannot be higher than maximum")

        self._max_value = value
        self.setValue(self.value())

    def minimum(self):
        return self._min_value

    def maximum(self):
        return self._max_value