I'd like to specify the steps that a QSlider can slide, like it is possible for the QSpinBox by using setSingleStep
. I tried to use setSingleStep of QAbstractSlider, but this seems to have no effect.
Any ideas?
Try setting the tickInterval
EDIT
Sorry for the tickInterval, didn't quite thinked about it, however i have this working code and it does what you want using setSingleStep
import sys
from PyQt4.QtGui import QApplication, QSlider, QMainWindow
class Window(QMainWindow):
def __init__(self, parent = None):
super(Window, self).__init__(parent)
slider = QSlider()
slider.setMinimum(0)
slider.setMaximum(100)
slider.setTickInterval(20)
slider.setSingleStep(20)
self.setCentralWidget(slider)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I extended the QSlider class to limit the user so that they cannot track the slider between the steps.
The SetInterval
method is equivalent to combining the setTickInterval
and setSingleStep
methods, but also stops the slider being positioned between tick values.
The class also allows the use of float values as slider limits or intervals and allows the index of the point selected on the slider to be set and read.
class DoubleSlider(qw.QSlider):
def __init__(self, *args, **kargs):
super(DoubleSlider, self).__init__( *args, **kargs)
self._min = 0
self._max = 99
self.interval = 1
def setValue(self, value):
index = round((value - self._min) / self.interval)
return super(DoubleSlider, self).setValue(index)
def value(self):
return self.index * self.interval + self._min
@property
def index(self):
return super(DoubleSlider, self).value()
def setIndex(self, index):
return super(DoubleSlider, self).setValue(index)
def setMinimum(self, value):
self._min = value
self._range_adjusted()
def setMaximum(self, value):
self._max = value
self._range_adjusted()
def setInterval(self, value):
# To avoid division by zero
if not value:
raise ValueError('Interval of zero specified')
self.interval = value
self._range_adjusted()
def _range_adjusted(self):
number_of_steps = int((self._max - self._min) / self.interval)
super(DoubleSlider, self).setMaximum(number_of_steps)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With