We are using Qt 5.10/C++ and I'm asked to implement a feature using the QSlider
class.
My colleague wants me to emit a signal, whenever the user performs a double mouse click on the slider handle.
How can this be achieved. Maybe I have to reimplement
bool event(QEvent *e)
, but I don't know how to start.
Working solution
With the help of the comments I derived a working solution:
#pragma once
#include <QSlider>
#include <QMouseEvent>
#include <QStyleOption>
#include <QDebug>
class DoubleClickSlider : public QSlider {
Q_OBJECT
public:
DoubleClickSlider(QWidget* parent = nullptr) : QSlider(parent) { };
signals:
void sliderHandleDoubleClicked();
protected:
void mouseDoubleClickEvent(QMouseEvent *event) override {
QStyleOptionSlider opt;
this->initStyleOption(&opt);
QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
if (sr.contains(event->pos())) {
qDebug() << "Double clicked handle";
emit sliderHandleDoubleClicked();
}
QSlider::mouseDoubleClickEvent(event);
}
};