PySide: SpinBox use a set of fixed values

213 Views Asked by At

I need a spinbox (or something similar) to have a fixed set of values to use. SetSingleStep does not do the trick because i need something like:

1 -> 5 -> 10 -> 100

Any suggestions?

1

There are 1 best solutions below

0
On BEST ANSWER

So i finally have a solution to the problem. I created a custom spinbox by subcalssing QSpinBox and overwriting the byStep method like so:

class SpinBoxSpecial(QtGui.QSpinBox):
def __init__(self, parent=None):
    super(SpinBoxSpecial, self).__init__(parent)

    self._step_range = [1, 5, 10, 100]
    self._index = 2 # default index - in this case '10'

def get_range(self):
    return self._step_range

def set_range(self, _range):
    self._step_range = _range

def get_index(self):
    return self._index

def set_index(self, _index):
    self._index = _index

def stepBy(self, step):
    value = self.value()
    super(SpinBoxSpecial, self).stepBy(step)
    if self.value() < value:
        if self._index > 0:
            self._index -= 1
    else:
        if self._index < len(self._step_range):
            self._index += 1
    self.setValue(self._step_range[self._index])

i hope this helps!