diff --git a/src/widget/clickableslider.hpp b/src/widget/clickableslider.hpp index a247ee5..6111d09 100644 --- a/src/widget/clickableslider.hpp +++ b/src/widget/clickableslider.hpp @@ -16,17 +16,34 @@ protected: void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { - int val; - if (orientation() == Qt::Horizontal) { - val = minimum() + (maximum() - minimum()) * event->pos().x() / width(); - } else { - val = minimum() + (maximum() - minimum()) * (height() - event->pos().y()) / height(); - } - setValue(val); + // Jump the handle to the clicked position first so Qt's own + // press handler sees the click "on" the handle and starts drag. + const int val = posToValue(event->pos()); + setSliderPosition(val); emit sliderMoved(val); - event->accept(); - } else { - QSlider::mousePressEvent(event); } + // Always forward so the slider enters drag mode normally. + QSlider::mousePressEvent(event); + } + + void mouseMoveEvent(QMouseEvent *event) override + { + // During a drag, keep snapping to cursor position. + if (event->buttons() & Qt::LeftButton) { + const int val = posToValue(event->pos()); + setSliderPosition(val); + emit sliderMoved(val); + } + QSlider::mouseMoveEvent(event); + } + +private: + int posToValue(const QPoint &pos) const + { + const int span = orientation() == Qt::Horizontal ? width() : height(); + const int pixel = orientation() == Qt::Horizontal ? pos.x() : (height() - pos.y()); + if (span <= 0) return minimum(); + const int val = minimum() + (maximum() - minimum()) * pixel / span; + return qBound(minimum(), val, maximum()); } };