Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Confusing eventFilter behaviour
Forum Updated to NodeBB v4.3 + New Features

Confusing eventFilter behaviour

Scheduled Pinned Locked Moved General and Desktop
eventeventfilter
2 Posts 2 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jakob
    wrote on last edited by Jakob
    #1

    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));
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved