Results 1 to 7 of 7

Thread: How to prevent Event Propagation to Parent?

  1. #1
    Join Date
    Mar 2007
    Location
    India
    Posts
    27
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question How to prevent Event Propagation to Parent?

    I have a class derived from QFrame inside which I have an object of a class derived from QWidget.
    Qt Code:
    1. ______________________________________________________
    2. | myFrameClass : public QFrame
    3. | {
    4. | Context Menu on RightClick defined here;
    5. | myWidgetClass->setParent(this);
    6. | }
    7. | ______________________________________
    8. | | myWidgetClass : public QWidget |
    9. | | { |
    10. | | mousePressEvent( QMouseEvent e) |
    11. | | { |
    12. | | e->ignore(); |
    13. | | } |
    14. | | } |
    15. | |______________________________________|
    16. |___________________________________________________
    To copy to clipboard, switch view to plain text mode 

    I have defined a context-menu to appear when I right-click on myFrameClass. And I have explicitly overridden the mousePressEvent inside myWidgetClass. In spite of this, when I right-click on myWidgetClass, the context-menu of myFrameClass pops up . I dont understand, why, if I have overridden to ignore the mousePressEvent, should the mousePressEvent go to the parent (myFrameClass)?

    Any pointers would be appreciated!
    Tnx in adv!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to prevent Event Propagation to Parent?

    The normal sequence is:
    Mouse press Event -> Mouse Move Event -> Mouse Release Event -> Context Menu Event.

    Anyway, you should accept an event in order to stop it from propagating further.

  3. #3
    Join Date
    Mar 2007
    Location
    India
    Posts
    27
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: How to prevent Event Propagation to Parent?

    Quote Originally Posted by marcel View Post
    The normal sequence is:
    Mouse press Event -> Mouse Move Event -> Mouse Release Event -> Context Menu Event.

    Anyway, you should accept an event in order to stop it from propagating further.
    What is this sequence for? I dint get you.
    I tried e->accept() as well but still the event propagates to the parent class myQFrameClass.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to prevent Event Propagation to Parent?

    Wait, are you using QWidget::mousePressEvent() or QWidget::contextMenuEvent() to show the context menu? It should be the latter. Accepting an event stops the event propagation.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2007
    Location
    India
    Posts
    27
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to prevent Event Propagation to Parent?

    Quote Originally Posted by jpn View Post
    Wait, are you using QWidget::mousePressEvent() or QWidget::contextMenuEvent() to show the context menu? It should be the latter. Accepting an event stops the event propagation.
    The code for showing the Context-Menu is implemented using mousePressEvent(). It's not my code ownership and is already existing so cant do much about it.

    But here the case is different I think. Coz the child object is myWidgetClass and the parent is myFrameClass. And in myWidgetClass I _am_ accepting() the event so meanging which it should not propagate further to its parent, right?

    Anyway, I'm looking into eventFilter(QObject *obj, QEvent *event) now. Hope I find something useful!

  6. #6
    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: How to prevent Event Propagation to Parent?

    This works quite well for me, so you must have messed something up.
    Please note that this approach is the worst you could take. The best would be to use the context menu event and a worse but still better than using mousePress is to use mouseReleaseEvent.
    Attached Files Attached Files

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

    viking (9th May 2007)

  8. #7
    Join Date
    Mar 2007
    Location
    India
    Posts
    27
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: How to prevent Event Propagation to Parent?

    Quote Originally Posted by wysota View Post
    This works quite well for me, so you must have messed something up.
    Please note that this approach is the worst you could take. The best would be to use the context menu event and a worse but still better than using mousePress is to use mouseReleaseEvent.
    thanks! i dug into the code. it _was_ implemented using contextMenuEvent. but in spite of that and the accept() in the overridden procedure, the contextMenu still pops up. This is very weird!

Similar Threads

  1. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  2. Passing event to parent
    By QPissedOff in forum Newbie
    Replies: 1
    Last Post: 26th April 2006, 16:37
  3. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

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.