[Interest] Forwarding an event to a child

Bo Thorsen bthorsen at ics.com
Wed Jan 29 12:57:10 CET 2014


Den 29-01-2014 09:27, Etienne Sandré-Chardonnal skrev:
> Dear all,
>
> I'm facing the following issue. I would like to forward events (keys,
> mouse, etc...) from a widget to a child widget. I have tried calling
> sendEvent(child, event) from reimplemented event handlers.
>
> However, if the child ignores the event, this is re-propagated to its
> parent, and this crashes due to recursion. Is there an elegant simple
> way to make this work?

You have three choices here, I think:

1) Un-protect the events that you need and call the event handler method 
directly:

class Child {
public:
   use keyPressEvent;
};

void Parent::keyPressEvent(event) {
   ...
   child->keyPressEvent();
}

2) Switch direction. Instead of trying to bend the Qt event system to do 
what you want, use normal methods:

class Child {
public:
   void handleKeyPress(event)
};

void Parent::keyPressEvent(event) {
   ...
   child->handleKeyPress();
}

3) Continue with your current sendEvent idea, but store the event 
pointer in the parent and check if you get an event resent.

My choice would be 2). I don't like doing weird things to something like 
events. You do not want to make a mistake in this area.

3) is only acceptable if you absolutely have to support unknown sets of 
children. It's a brittle approach and the one that's most likely to go 
bad at some point.

Bo.

-- 
Bo Thorsen, European Engineering Manager, ICS
Integrated Computer Solutions. Delivering World-Class Applications
http://ics.com/services



More information about the Interest mailing list