Results 1 to 9 of 9

Thread: Event filter: very confused about how they work

  1. #1
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Event filter: very confused about how they work

    I've read documentation about event filters and have been have to install a filter for a glFrame control, that triggers on keypress events.

    But I can't see how to apply the same event filter for another control, because from what I can understand it seems like one has to create a new class for it.

    Could you please give me some hints? Thank you very much...

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Event filter: very confused about how they work

    You can simply install the same class as event filter for any other widget:
    Qt Code:
    1. otherWidget->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

  3. The following user says thank you to Oleg for this useful post:

    papillon (19th November 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Event filter: very confused about how they work

    Thanks Oleg. This lead me to another question: how would you create 2 or more filters, and install those for widgets of the same class?

  5. #4
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Event filter: very confused about how they work

    If you want to handle events in two or more different event filters, then you should simply install all of the filters for given widgets and be sure to return false from eventFilter functions to prevent end of event processing.
    Oleg Shparber

  6. The following user says thank you to Oleg for this useful post:

    papillon (19th November 2011)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,386
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Event filter: very confused about how they work

    You want two or more event filters for the *same* widget? Or are you asking whether you can have two or more different event filters, and install one on PushButton1 and another on PushButton2, etc.? Both can be done.

    Event filters are installed on an instance by instance basis. They affect only the specific QObject instance that they are installed on.

    You can have multiple event filters per object instance. The one that was installed last gets called first.
    You can use the same event filter for multiple object instances. The eventFilter() method tells you which object is the one to which the event applies.

    An event filter is basically telling the event loop, "Before you send the event to the object that should receive it, send it to me first, and I can look at it and tell you what to do with it". The return value from the event filter is what tells the event loop whether to pass the event along to the actual receiver or to eat it. If multiple filters are installed on the same object instance, then each one of them gets a whack at it (unless an event filter earlier in the chain killed it). If no one kills the event, then the actual object (like the push button) finally gets to see it.

    Note that the Graphics / View framework does things a bit differently. Events get sent to the scene first, then forwarded to the appropriate graphics item. If you install an event filter on the QGraphicsView, it never gets invoked. There is a way around this (subclass QGaphicsView and in the event handler, ignore() the event). But I am pretty sure that having to do this means a misunderstanding on my part about how the G/V frameworks handles events, so I still have some learning to do.
    Last edited by d_stranz; 19th November 2011 at 16:53.

  8. The following user says thank you to d_stranz for this useful post:

    papillon (19th November 2011)

  9. #6
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Event filter: very confused about how they work

    I would like to define and use multiple filters on different widgets. So for example, have a filter installed to a QFrame to intercept when the mouse is over, and a another event filter to another QFrame to intercept other actions.

  10. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,386
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Event filter: very confused about how they work

    So all you have to do is something like this:

    Qt Code:
    1. class Filter1 : public QObject
    2. {
    3. Q_OBJECT;
    4.  
    5. public:
    6. bool eventFilter( QObject * object, QEvent * event )
    7. {
    8. //... examine the event and return true or false
    9. }
    10. };
    11.  
    12. class Filter2 : public QObject
    13. {
    14. Q_OBJECT;
    15.  
    16. public:
    17. bool eventFilter( QObject * object, QEvent * event )
    18. {
    19. //... examine the event and return true or false
    20. }
    21. };
    22.  
    23. // Somefile.cpp
    24.  
    25. QFrame * frame1 = new QFrame( this );
    26. QFrame * frame2 = new QFrame( this );
    27.  
    28. Filter1 * filter1 = new Filter1( this );
    29. Filter2 * filter2 = new Filter2( this );
    30.  
    31. frame1->installEventFilter( filter1 );
    32. frame2->installEventFilter( filter2 );
    To copy to clipboard, switch view to plain text mode 

    So now all events for frame1 will be sent first to the filter1 instance of Filter1, likewise for frame2. If either filter eats the event, the respective frame will not see it. Otherwise, after the filter is done with it, the event will then be forwarded to the frame. In the eventFilter() method for filter1, the QObject argument will point to frame1, likewise for filter2.

  11. The following user says thank you to d_stranz for this useful post:

    papillon (19th November 2011)

  12. #8
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Event filter: very confused about how they work

    Ah that how is done! Thanks so much, I was quite lost trying to find that out!

  13. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,386
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Event filter: very confused about how they work

    Note that the Graphics / View framework does things a bit differently. Events get sent to the scene first, then forwarded to the appropriate graphics item. If you install an event filter on the QGraphicsView, it never gets invoked. There is a way around this (subclass QGaphicsView and in the event handler, ignore() the event). But I am pretty sure that having to do this means a misunderstanding on my part about how the G/V frameworks handles events, so I still have some learning to do.
    I wrote this earlier, but reading another post lead me to the solution: For a QGraphicsView, install the even filter on the QGraphicsView's viewport() widget, not on the QGraphicsView itself. If you do this, the event filter works as expected. Learning accomplished!

Similar Threads

  1. Event filter with mouse events
    By felo188 in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2011, 10:57
  2. Event filter question
    By d_stranz in forum Qt Programming
    Replies: 7
    Last Post: 7th July 2011, 23:08
  3. Replies: 0
    Last Post: 28th August 2010, 14:22
  4. problem with event filter compile for S60
    By jimiq in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 6th July 2010, 19:48
  5. Event Filter & No Focus problem
    By AlexanderPopov in forum Newbie
    Replies: 0
    Last Post: 22nd December 2009, 20:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.