PDA

View Full Version : Relaying a keyPressEvent from one Widget to another



bbdaffy
5th October 2010, 11:53
Hi,

I need to sort of "relay" the keypress events from a widget (x) to another widget (y). Widget x does not contain any child. What it does is simply capture keypress events. Widget y contains a more complicated array of child widgets such as buttons, labels etc. Some of the buttons in Widget y have registered shortcuts. What I want to do is basically when the focus is on Widget x, whatever key I pressed, I can re-trigger it on widget y, so that the relevant buttons (with the relevant shortcut key) gets to handle it as usual.

Hope that someone can help on this. Thanks a lot.

Live
5th October 2010, 13:09
You could look into

void QObject::installEventFilter ( QObject * filterObj )
to apply your filter (y) on your other widget (x)

You would have to reimplement

bool eventFilter(QObject *obj, QEvent *event)
this method in your widget (y) and call

x->installEventFilter(y);

Hope this helps.

aamer4yu
5th October 2010, 13:15
You can simply emit a event from widget x to widget y.. and handle it in a slot of widget y.
But a question... whats the purpose of widget x ? If you have proper shortcuts assigned, you don't need a widget x to capture them.

bbdaffy
6th October 2010, 03:01
Hi,

Thanks for all ur replies. I'll try out what you suggested. As to aamer4yu's question, it's kind of a long story. :p Basically widget x is there as a "translucent screen" over a third party window (non-qt) embedded in my Qt Window to prevent it from receiving keyboard and mouse clicks. And effectively I want to direct the keyboard and mouse clicks intercepted by widget x to the main widget y so that they can be handled by the appropriate controls. For example, in widget y I have a button which registered shortcut F3. so when my focus is on widget x, and I press F3, I'm hoping that this button can handle it as if widget y is in focus. And because there are so many buttons within widget y with shortcuts, I was thinking the easiest way to do this is to sort of re-trigger the keypress event in widget y. Hope this makes sense. Anyway thanks, will come back to this thread if I encounter problems in my resolution. :)

bbdaffy
6th October 2010, 10:02
Hi,

I tried to implement the eventFilter method. So I managed to receive the keypress events in my widget y. However, I'm not so sure how to handle it from there. I tried calling QWidget::eventFilter() but my buttons din seem to be receiving the event. Maybe let me describe my widget y in more detail. Widget y contains a few widgets which serves as containers for many buttons. Each of these buttons has a QShortcut associated, namely F1 to F10. There is also a QStackedWidget containing many pages, each of which contains buttons. Some of the buttons in the different pages even have overlapping QShortcuts. For example, "Ctrl+A" could be used for button A in page 1 as well as button B in page 2 and button C in page 3 etc. So what is the fastest and simplest way for the respective keypress events from widget x to be sent to the respective buttons in widget y to be handled as if someone actually pressed this key with the focus on widget y?

Sorry if my question sounds confusing. Look forward for your replies. Thanks a lot. :D