Q滑块步进?

发布于 2024-10-14 18:18:12 字数 138 浏览 6 评论 0原文

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

并安 2024-10-21 18:18:12

尝试设置 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_())

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_())
水水月牙 2024-10-21 18:18:12

我扩展了 QSlider 类来限制用户,以便他们无法跟踪步骤之间的滑块。
SetInterval 方法相当于组合 setTickIntervalsetSingleStep 方法,但也会阻止滑块位于刻度值之间。

该类还允许使用浮点值作为滑块限制或间隔,并允许设置和读取滑块上所选点的索引。

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)

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)
苏大泽ㄣ 2024-10-21 18:18:12

我的解决方案是使用信号 sliderMoved 并纠正位置。下面的示例将滑块移动到值 50 或 100

import sys
from PyQt5.QtGui import QApplication, QSlider, QMainWindow

class Window(QMainWindow):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        self.slider = QSlider(self)
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)

        self.slider.setTickInterval(20)
        self.slider.setSingleStep(20)

        self.slider.sliderMoved.connect(self.correct_pos)    

        self.setCentralWidget(self.slider)
     
    def correct_pos(self, pos):
        if pos < 50:
            corrected = 50
        else:
            corrected = 100
        self.slider.setValue(corrected)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

My solution is using signal sliderMoved and correct the position. The below example moves slider to value 50 or 100

import sys
from PyQt5.QtGui import QApplication, QSlider, QMainWindow

class Window(QMainWindow):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        self.slider = QSlider(self)
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)

        self.slider.setTickInterval(20)
        self.slider.setSingleStep(20)

        self.slider.sliderMoved.connect(self.correct_pos)    

        self.setCentralWidget(self.slider)
     
    def correct_pos(self, pos):
        if pos < 50:
            corrected = 50
        else:
            corrected = 100
        self.slider.setValue(corrected)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
错爱 2024-10-21 18:18:12

setSingleStep() 现在仍然无法在 PyQt5 中的滑块上工作,

所以我尝试这样做来达到相同的效果,

your_slider = QtWidgets.QSlider()
your_slider.valueChanged.connect(lambda:set_step(step))
flag = 0
def your_func():
    pass
    #func here
def set_step(step):
    value = your_slider.value()//step*step
    your_slider.setValue(value)
    if flag != value:
        flag = value
        your_func()
    

我刚刚学了几个月的 python

如果有错误请纠正

setSingleStep() still don't work on slider in PyQt5 now

so I tried this to make the same effect

your_slider = QtWidgets.QSlider()
your_slider.valueChanged.connect(lambda:set_step(step))
flag = 0
def your_func():
    pass
    #func here
def set_step(step):
    value = your_slider.value()//step*step
    your_slider.setValue(value)
    if flag != value:
        flag = value
        your_func()
    

I just learned python for several months

Please correct it if there is a mistake

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文