c++qteventsdouble-clickmousepress

Why always calling mousePressEvent before mouseDoubleClickEvent


please consider the following code:

#include <QWidget>
#include <iostream>
#include <QApplication>

class Widget : public QWidget
{
public:
    void mousePressEvent(QMouseEvent* event)
    {
        std::cout << "mousePressEvent" < std::endl;
    }

    void mouseDoubleClickEvent(QMouseEvent* event)
    {
        std::cout << "mouseDoubleClickEvent" << std::endl;
    }
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    Widget w;
    w.show();
    return app.exec();
}

Every time I process double click, the output is:

mousePressEvent
mouseDoubleClickEvent

This means Qt always call mousePressEvent as soon as one press proceed without waiting the second press. Is there a way to turn off this option, so that no mousePressEvent call will perform in case of double-click.


Solution

  • I would bypass the handling of single click event (using a QTimer) by the period of time equal to the QApplication::doubleClickInterval() value. If double click is not happened during that time, I should handle "single click", otherwise the double click should be processed.