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.
Code:
______________________________________________________
| myFrameClass
: public QFrame| {
| Context Menu on RightClick defined here;
| myWidgetClass->setParent(this);
| }
| ______________________________________
| | myWidgetClass
: public QWidget |
| | { |
| | { |
| | e->ignore(); |
| | } |
| | } |
| |______________________________________|
|___________________________________________________
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:eek: . :confused: I dont understand, why, if I have overridden to ignore the mousePressEvent, should the mousePressEvent go to the parent (myFrameClass)? :confused:
Any pointers would be appreciated!
Tnx in adv!
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.
Re: How to prevent Event Propagation to Parent?
Quote:
Originally Posted by
marcel
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.
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.
Re: How to prevent Event Propagation to Parent?
Quote:
Originally Posted by
jpn
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!
1 Attachment(s)
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.
Re: How to prevent Event Propagation to Parent?
Quote:
Originally Posted by
wysota
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!