Results 1 to 5 of 5

Thread: Relaying a keyPressEvent from one Widget to another

  1. #1
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Relaying a keyPressEvent from one Widget to another

    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.

  2. #2
    Join Date
    Sep 2010
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Relaying a keyPressEvent from one Widget to another

    You could look into
    Qt Code:
    1. void QObject::installEventFilter ( QObject * filterObj )
    To copy to clipboard, switch view to plain text mode 
    to apply your filter (y) on your other widget (x)

    You would have to reimplement
    Qt Code:
    1. bool eventFilter(QObject *obj, QEvent *event)
    To copy to clipboard, switch view to plain text mode 
    this method in your widget (y) and call
    Qt Code:
    1. x->installEventFilter(y);
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.

  3. The following user says thank you to Live for this useful post:

    bbdaffy (6th October 2010)

  4. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Relaying a keyPressEvent from one Widget to another

    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.

  5. The following user says thank you to aamer4yu for this useful post:

    bbdaffy (6th October 2010)

  6. #4
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Relaying a keyPressEvent from one Widget to another

    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. 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.

  7. #5
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Relaying a keyPressEvent from one Widget to another

    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.

Similar Threads

  1. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 02:12
  2. relaying signals
    By Cruz in forum Qt Programming
    Replies: 7
    Last Post: 6th April 2010, 17:59
  3. prob with keyPressEvent()
    By Askar in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2009, 07:47
  4. keyPressEvent issue
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 15:39
  5. keypressevent with pyqt4
    By coldlin in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2007, 16:52

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.