How to emit a signal if double clicking on slider handle

794 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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

    }
};