QSlider踩?

时间:2011-01-28 11:51:18

标签: python qt pyqt qslider qabstractslider

我想指定QSlider可以滑动的步骤,就像QSpinBox可以使用setSingleStep一样。我试图使用QAbstractSlider的setSingleStep,但这似乎没有效果。

有什么想法吗?

2 个答案:

答案 0 :(得分:6)

尝试设置tickInterval

修改

很抱歉tickInterval,我没有想到它,但是我有这个工作代码,它使用 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_())

答案 1 :(得分:0)

我扩展了QSlider类以限制用户,以使他们无法在步骤之间跟踪滑块。 SetInterval方法等效于组合setTickIntervalsetSingleStep方法,但也可以使滑块停止在刻度值之间。

该类还允许将float值用作滑块限制或间隔,并允许设置和读取在滑块上选择的点的索引。

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)