QSlider mouse direct jump

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

    I needed this too and tried spyke solution, but it's missing two things:

    • inverted appearance
    • handle picking (when the mouse is over the handle, direct jump is not necessary)

    So, here's the reviewed code:

    void MySlider::mousePressEvent ( QMouseEvent * event )
    {
      QStyleOptionSlider opt;
      initStyleOption(&opt);
      QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
    
      if (event->button() == Qt::LeftButton &&
          sr.contains(event->pos()) == false)
      {
        int newVal;
        if (orientation() == Qt::Vertical)
           newVal = minimum() + ((maximum()-minimum()) * (height()-event->y())) / height();
        else
           newVal = minimum() + ((maximum()-minimum()) * event->x()) / width();
    
        if (invertedAppearance() == true)
            setValue( maximum() - newVal );
        else
            setValue(newVal);
    
        event->accept();
      }
      QSlider::mousePressEvent(event);
    }
    
    0 讨论(0)
  • 2020-12-23 16:58

    i have been trying and searching this on net and was expecting Qt for a smarter way doing this, unfortunately there was not big help (may be i was not searching properly )

    well i have done this in Qt creator:

    1. Add an eventFilter in header ( takes QObject and QEvent as argument ) (bool return type)
    2. Initialize in constructor ..for eg .. if ur slider is HSlider then ui->HSlider->installEventFilter(this);
    3. In the defination :

      a. check if the object is your slider type something like : ui->HSlider == Object

      b. Check for mouse click event something like : QEvent::MouseButtonPress == event->type

      c. if the above all passes means u have got mouse event on the slider do something like : in definition : ui->HSlider->setValue( Qcursor::pos().x() - firstval ); return QMainWindow::eventFilter(object, event);

    Note: fistVal : can be taken out by printing the cursur position at 0 = initial position of the slider ( with help of QCursor::pos().x() )

    hope this helps

    0 讨论(0)
  • 2020-12-23 17:04

    I think,

    the QStyle::sliderValueFromPosition() function can be used.

    http://qt-project.org/doc/qt-5/qstyle.html#sliderValueFromPosition

    0 讨论(0)
  • 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);
      }
    };
    
    0 讨论(0)
  • 2020-12-23 17:14

    A simple method would be to derive from QSlider and reimplement mousePressEvent(....) to set the marker position using setSliderPosition(int).

    0 讨论(0)
  • 2020-12-23 17:15

    Here is a simple implementation in python using QStyle.sliderValueFromPosition():

    class JumpSlider(QtGui.QSlider):
    
        def mousePressEvent(self, ev):
            """ Jump to click position """
            self.setValue(QtGui.QStyle.sliderValueFromPosition(self.minimum(), self.maximum(), ev.x(), self.width()))
    
        def mouseMoveEvent(self, ev):
            """ Jump to pointer position while moving """
            self.setValue(QtGui.QStyle.sliderValueFromPosition(self.minimum(), self.maximum(), ev.x(), self.width()))
    
    0 讨论(0)
提交回复
热议问题