qtqslider

QSlider mouse direct jump


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 ?


Solution

  • Well, I doubt that Qt has a direct function for this purpose.

    Try to use custom widgets. This should work!

    Try the following logic

    class MySlider : public QSlider
    {
    
    protected:
      void mousePressEvent ( QMouseEvent * event )
      {
        if (event->button() == Qt::LeftButton)
        {
            if (orientation() == Qt::Vertical)
                setValue(minimum() + ((maximum()-minimum()) * (height()-event->y())) / height() ) ;
            else
                setValue(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
    
            event->accept();
        }
        QSlider::mousePressEvent(event);
      }
    };