PDA

View Full Version : How to prevent Event Propagation to Parent?



viking
8th May 2007, 07:43
I have a class derived from QFrame inside which I have an object of a class derived from QWidget.

__________________________________________________ ____
| myFrameClass : public QFrame
| {
| Context Menu on RightClick defined here;
| myWidgetClass->setParent(this);
| }
| ______________________________________
| | myWidgetClass : public QWidget |
| | { |
| | mousePressEvent( QMouseEvent e) |
| | { |
| | 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!

marcel
8th May 2007, 08:01
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.

viking
8th May 2007, 08:04
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.

jpn
8th May 2007, 08:05
Wait, are you using QWidget::mousePressEvent() or QWidget::contextMenuEvent() to show the context menu? It should be the latter. Accepting an event (http://doc.trolltech.com/4.2/qevent.html#accept) stops the event propagation.

viking
8th May 2007, 08:12
Wait, are you using QWidget::mousePressEvent() or QWidget::contextMenuEvent() to show the context menu? It should be the latter. Accepting an event (http://doc.trolltech.com/4.2/qevent.html#accept) 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!

wysota
8th May 2007, 11:09
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.

viking
9th May 2007, 06:29
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!