QSlider mouse direct jump

后端 未结 12 1769
爱一瞬间的悲伤
爱一瞬间的悲伤 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:11

    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);
      }
    };
    

提交回复
热议问题