fix: make volume/progress sliders draggable
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -16,17 +16,34 @@ protected:
|
|||||||
void mousePressEvent(QMouseEvent *event) override
|
void mousePressEvent(QMouseEvent *event) override
|
||||||
{
|
{
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
int val;
|
// Jump the handle to the clicked position first so Qt's own
|
||||||
if (orientation() == Qt::Horizontal) {
|
// press handler sees the click "on" the handle and starts drag.
|
||||||
val = minimum() + (maximum() - minimum()) * event->pos().x() / width();
|
const int val = posToValue(event->pos());
|
||||||
} else {
|
setSliderPosition(val);
|
||||||
val = minimum() + (maximum() - minimum()) * (height() - event->pos().y()) / height();
|
|
||||||
}
|
|
||||||
setValue(val);
|
|
||||||
emit sliderMoved(val);
|
emit sliderMoved(val);
|
||||||
event->accept();
|
}
|
||||||
} else {
|
// Always forward so the slider enters drag mode normally.
|
||||||
QSlider::mousePressEvent(event);
|
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());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user