QApplication, listen to all mice clicks...



  • Hi all,

    I have developed an Qt/QML application for embedded devices and all works fine.
    Some of those devices are used a resistive touch panel which has to be re-calibrated relative often, depending on hardware quality and temperature/humidity.

    To not annoying users with touch panel calibration on each application start, I would like to start calibration tool when user hits 10 times the screen in less than 5 seconds.
    Is there a way to capture all click events on application level (QApplication, perhaps QGuiApplication??) to be able to count mice clicks and measure elapsed time?

    Thanks for any suggestion

    Regards

    Fabrice



  • I reply to my self, perhaps there is someone else which have the same kind of issue ;-)

    In fact, it is quite easy, the solution is to use install an event filter on application level:

    class MouseEventFilter : public QObject
    {
        Q_OBJECT
    public:
        explicit MouseEventFilter(QObject *parent = 0);
    
    signals:
        void recalibrateTouch();
        
    public slots:
    
        // QObject interface
    public:
        bool eventFilter(QObject *object, QEvent *event)
        {
            if(event->type() == QEvent::MouseButtonPress)
            {
                if(m_delay.hasExpired(5000))
                {
                    m_clickCounter=0;
                    m_delay.start();
                }
                ++m_clickCounter;
                if(m_clickCounter == 10)
                    emit recalibrateTouch();
            }
            return QObject::eventFilter(object,event);
        }
        
    private:
        int m_clickCounter;
        QElapsedTimer m_delay;
    };
    

    Then simply install the event filter on application level like this:

    app->installEventFilter(new MouseEventFilter(qApp));
    

    That's it :)
    Regards