Results 1 to 7 of 7

Thread: Event filtering

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Event filtering

    Hello!

    I have three widgets stacked. A is parent of B and B is parent of C. All mouse and keyboard events should go to A. I have installed an event filter in A for B and that works, A gets everything before B does. I also installed a filter in B for C, so now B gets all the clicks meant for C. But they don't go to A! And I don't want to install A as an event filter for C explicitly, because A is not supposed to know about C. How can I do this?

    Thanks
    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Event filtering

    Sounds like a bad design problem.
    You have conflicting requirements: A needs to filter C's events without knowing about C.
    The solution should be a design solution not implementation workaround.

    One work around is to send signals from the event filter in B - but I suggest you rethink your deign.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Event filtering

    Why do you want to reverse the natural event propagation scheme?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Event filtering

    Well, we could discuss if the program structure makes sense or not, but that would be a different topic. There are cases when you don't have the time to refactor. The situation is that this issue is a part of a large software and a workaround has to be found TODAY. Anything else is secondary. Nobody cares what the code looks like inside.

    In a nutshell: the troubled software is about keyframe interpolation for a robotic arm. A sequence of keyframes defines a motion. Keyframe widgets are lined up in a QScrollArea such that they can be dragged and dropped with the mouse to change their order. The QScrollArea (or more like its widget) is the parent A, that holds Keyframe objects, that are Bs. Each Keyframe object also shows a 3D GLWidget of the robot in the current pose, these are the children C. Now the GLWidgets receive mouse input so that they can be rotated and panned, which is needed in other parts of the whole software. But not in this case. When the Keyframes are in the scroll area, every mouse action has to be routed up from C to A and from B to A because A handles the drag and drop feature.

    So please, can anyone suggest one or two lines of code that somehow solves this problem for now and worry about rewriting months of work later? I installed an event filter in the Keyframes already that eats the mouse clicks from the GLWidget. I just don't know how to resend the events so that the scroll area gets them.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Event filtering

    Well, then that's easy. You don't want A to get C's events before C gets it, you want A to get them instead of C. The simplest way is to add a flag to B and C that they should ignore input events and in appropriate event handlers (mouse{Press,Move,Release}Event, key{Press,Release}Event) call QEvent::ignore() on the event object you get. Then events from C will be propagated to the parent (B) which will ignore them again and they'll reach A where you can handle them in the regular event handlers.

    Qt Code:
    1. class C : public ... {
    2. public:
    3. void setIgnoreInputEvents(bool v) { m_ignoreInput = v; }
    4. protected:
    5. void mousePressEvent(QMouseEvent *ev) {
    6. if(m_ignoreInput) { ev->ignore(); return; }
    7. // do your regular stuff here
    8. }
    9. private:
    10. bool m_ignoreInput;
    11. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Cruz (28th March 2011)

  7. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Event filtering

    Beautiful, thank you! It works. I especially like that the special handling starts exactly where one would expect, so it can be documented in the source ow things work and why they are messy.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Event filtering

    Just for completeness - there is an alternative that doesn't require you to modify B and C. You can install an event filter on the application object to intercept all events in your application. Then you can check if the target of the event is your own descendant and if so, steal the event. But this solution is slower than the one suggested earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Key filtering
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2010, 09:10
  2. Filtering QSqlQueryModel
    By mourad in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2009, 09:32
  3. mouse move event filtering in PyQt4
    By lucaf in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2009, 14:53
  4. Event Filtering on Widgets
    By QbelcorT in forum Newbie
    Replies: 1
    Last Post: 28th November 2008, 21:27
  5. Filtering a TreeWidget
    By soul_rebel in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2008, 20:08

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.