Important: Please read the Qt Code of Conduct -

[Solved] eventFilter appears to be not working pressing mouse button over a Qt button widget...

  • Hi...

    We have been investing time into eventFilter trying to capture every mouse action in order to control a watch dog timer. The problem is that no matter how the eventFilter is inserted, and no matter how many places we insert it, we only see a QEvent::MouseButtonPress event when the mouse button is presses over the Qt window where there is no Qt button widget defined.

    I had thought the eventFilter would pick up every mouse button press. I thought I would simply kick the watch dog and return false so that Qt and our code would process the button press as originally intended.

    But no matter what we have tried, we only see mouse button presses where there are no buttons to press. Almost exactly what we did not want (certainly not what we need).

    Does anyone have ideas as to why the eventFilter is not receiving the mouse events first? Really, what is the point of filter events if you can not stop something before Qt processes it?


  • You can stop a mouse button press on its clicked/pressed/released signal, just add an if and returns...
    I'm having some trouble with the eventFilter aswel, sometimes it does not work as expected :S

  • To get all of the applications mouse events you would need to install the event filter on the application object.

    @class MainWindow : public QMainWindow, private Ui_MainWindow
    MainWindow( QWidget *parent = 0, Qt::WindowFlags f = 0 );

         bool eventFilter(QObject *obj, QEvent *event);


    @MainWindow::MainWindow( QWidget *parent, Qt::WindowFlags f ) :
    QMainWindow( parent, f )
    setupUi( this );
    qApp->installEventFilter( this );


    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    switch( event->type() ){
    case QEvent::MouseButtonDblClick:
    qDebug() << "Mouse Button Double Clicked";
    case QEvent::MouseButtonPress:
    qDebug() << "Mouse Button Pressed";
    case QEvent::MouseButtonRelease:
    qDebug() << "Mouse Button Released";
    // pass the event on to the parent class
    return QMainWindow::eventFilter(obj, event);

  • @Richard Neal
    Ah! I did not realize "qApp" existed let alone could be used here. That's what was missing! Simple now. Impossible yesterday!


  • Hi,
    I am trying same program but i am not getting window

    I am get like "The program has unexpectedly finish"
    window.exe exited with code -1073741819

    plz help me.

Log in to reply