Confusing eventFilter behaviour



  • The setup is as follows:

    • Qt 5.4.0 on Win7
    • There is an event handling object, which is created a child object of qApp
    • I install an eventFilter on qApp
    • I send events to the event handling object

    When doing the last, I was expecting that first the event() function on the event handling object was executed, and only if that function returns false, the event would be propagated to the parent, hence ending up first in the filter, and only if that filter would return false, end up in the event() function of qApp.

    What is happening however: the event ends up immediately in the event filter.

    A bug? Some misunderstanding on my part?

    I created the following minimal example, where I expected it to be impossible to ever reach the part that says IMPOSSIBLE. However, in reality when running the code and setting breakpoints both in the filter and the event handler, I see that I immediately end up in the filter.

    struct ObjectHandler : public QObject
    {
        ObjectHandler(QEvent::Type userType) : QObject(qApp), d_userType(userType) {}
        bool event(QEvent* event)
        {
            if (event->type() == d_userType)
            {
                return true;
            }
            return QObject::event(event);
    
        }
        QEvent::Type d_userType;
    };
    
    struct FilterObject : public QObject
    {
        FilterObject(QEvent::Type type) : QObject(nullptr), d_userType(type) {}
        bool eventFilter(QObject* object, QEvent* event)
        {
            if (event->type() == d_userType)
            {
                // IMPOSSIBLE??
                return true;
            }
            return false;
        }
    
        QEvent::Type d_userType;
    };
    
    TestHelper::QApplicationFixture appFixture;
    QEvent::Type userType = static_cast<QEvent::Type>(QEvent::registerEventType());
    ObjectHandler* handler = new ObjectHandler(userType);
    FilterObject filter(userType);
    qApp->installEventFilter(&filter);
        
    QCoreApplication::sendEvent(handler, new QEvent(userType));
    

  • Lifetime Qt Champion

    Hi,

    What you do is to send your event to the event dispatcher for it to dispatch said event to the correct object but at the same time you're setting your filter between the dispatcher and the rest of your application. Thus you are seeing the "impossible"

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.