Results 1 to 11 of 11

Thread: Space key doesn't trigger keyPressEvent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by wysota View Post
    As long as child widgets do not handle key events themselves...
    I don't know why you assume you have full information about my application, but to set this straight, the application contains widgets that do their own key event handling in undesired ways. For example, QSliders handle the arrow keys when they have focus. But the arrow keys have a different function in my program and it's important that the arrow keys do what they do no matter where the focus is. I would have to subclass QSlider or filter events or do whatever acrobatics is necessary to stop them from handling keys I need for something else. This goes for a number of widgets, all of which would require individual attention.

    Then there are also lovely cases where more than one widget needs to do something in response to a single key press, no matter where the focus is. As far as I can tell this is also not something that "proper" key handling is prepared for, because the widget that has the focus and handles the event, consumes the event too. So I would have to "hack" something to circumvent the default event handling mechanism and to forward the event to multiple widgets. Just because there are predefined Qt ways to accomplish this, it doesn't make it any less complex and cumbersome.

    The grabKeyboard() hack solves it all with one line and puts all keyboard handling in one place. It's easy to find, to understand, and to maintain.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by Cruz View Post
    The grabKeyboard() hack solves it all with one line and puts all keyboard handling in one place. It's easy to find, to understand, and to maintain.
    I still think this is a hack. The sort of "offcial" way of providing your own event handling is to reimplement QCoreApplication::notify().

    If grabKeyboard() works for you then that's fine. However if you are only after modifying some keys globally then using either of the two mentioned solutions (notify() or application event filter) is a more clean approach without getting you into a God Object design anti-pattern. Simply delegate the "custom" behaviours to a dedicated place and leave everything else to the default mechanism.

    E.g.:

    Qt Code:
    1. bool XXX::eventFilter(QObject *object, QEvent *event) {
    2. if(event->type() != Qt::KeyPressEvent) return false;
    3. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    4. if(keyEvent->key() == Qt::Key_Left && object != myMainWindow) {
    5. myMainWindow->keyPressEvent(keyEvent); // or do whatever else you'd like, e.g. event->ignore() letting it propagate until the main window
    6. return true; // eat the left arrow key event
    7. }
    8. return false;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 20th September 2014 at 22:19. Reason: typo
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Cruz (20th September 2014)

Similar Threads

  1. keyPressEvent doesn't work
    By 8Observer8 in forum Newbie
    Replies: 8
    Last Post: 27th August 2014, 05:29
  2. QTableView empty space or too little space.
    By davemar in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 16:00
  3. Replies: 1
    Last Post: 26th July 2009, 15:08
  4. QComboBox as a trigger
    By ape in forum Newbie
    Replies: 8
    Last Post: 4th February 2008, 08:57
  5. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

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
  •  
Qt is a trademark of The Qt Company.