QSlider mouse direct jump

后端 未结 12 1780
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 16:27

Instead of stepping when the user clicks somewhere on the qslider I want to make the slider jump to that position. How can this be implemented ?

12条回答
  •  不思量自难忘°
    2020-12-23 17:16

    This modification to the JumpSlider above works in PyQt5:

    class JumpSlider(QSlider):
        def _FixPositionToInterval(self,ev):
            """ Function to force the slider position to be on tick locations """
    
            # Get the value from the slider
            Value=QStyle.sliderValueFromPosition(self.minimum(), self.maximum(), ev.x(), self.width())
    
            # Get the desired tick interval from the slider
            TickInterval=self.tickInterval()
    
            # Convert the value to be only at the tick interval locations
            Value=round(Value/TickInterval)*TickInterval
    
            # Set the position of the slider based on the interval position
            self.setValue(Value)
    
        def mousePressEvent(self, ev):
            self._FixPositionToInterval(ev)
    
        def mouseMoveEvent(self, ev):
            self._FixPositionToInterval(ev)
    

提交回复
热议问题