-1

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.

Aleph0
  • 5,816
  • 4
  • 29
  • 80
  • 2
    There is a [`QWidget::mouseDoubleClickEvent()`](http://doc.qt.io/qt-5/qwidget.html#mouseDoubleClickEvent). According to doc., it doesn't seem to be overloaded in [`QSlider`](http://doc.qt.io/qt-5/qslider.html). If deriving from `QSlider` is an option, you may overload it in a derived class. (Otherwise, you may look for [event filters](http://doc.qt.io/qt-5/eventsandfilters.html#event-filters).) – Scheff's Cat Sep 28 '18 at 07:48
  • @Scheff: Thanks for the hint. I'll give it a try. Deriving from QSlider is an option for me. – Aleph0 Sep 28 '18 at 07:50
  • You're welcome. If you manage to make a working minimal example, don't hesitate to write a self-answer. ;-) – Scheff's Cat Sep 28 '18 at 07:52
  • 1
    As an alternative to overriding `QWidget::mouseDoubleClickEvent` you could [install an event filter](http://doc.qt.io/qt-5/qobject.html#eventFilter) on the `QSlider`. – G.M. Sep 28 '18 at 08:06
  • @Scheff: I tried using `QWidget::mouseDoubleClickEvent()` and there is an reaction to a mouse double click. Unfortunately, there is also an reaction to the double click, in case I'm not clicking on the slider handle. How can I separate the double click on the slider handle from other double clicks? – Aleph0 Sep 28 '18 at 08:08
  • 1
    In such cases, I have a look into `QStyle`: [Styles and Style Aware Widgets](http://doc.qt.io/qt-5/style-reference.html). There are many methods to draw controls but you will find also methods to retrieve rectangles of sub-controls. (Sorry for being not more specific - I'm a bit in hurry.) – Scheff's Cat Sep 28 '18 at 08:13
  • @Scheff: No problem. You already helped a a lot. I just needed a starting point. – Aleph0 Sep 28 '18 at 08:14
  • Please, don't edit the answer into the question. Write it as (self) answer (and roll back your question to previous version). ([This is the recommended way](https://stackoverflow.com/help/self-answer).) After 48 hours you may accept it as _the_ answer. Btw. it could be worth for additional rep. points if other stumble into it and find it valuable. – Scheff's Cat Sep 29 '18 at 06:45
  • @Scheff: Many thanks for the hint. I just answered my own question. – Aleph0 Oct 01 '18 at 05:44

1 Answers1

3

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

    }
};
Aleph0
  • 5,816
  • 4
  • 29
  • 80