Results 1 to 4 of 4

Thread: Global key press handling: best practice?

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    100
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Global key press handling: best practice?

    Hi all,

    got a small design problem here. My app consists of a typical widget hierarchy, with a centered main widget and around it several widgets with editable properties. I now want to react on some special keys (including normal characters like xyz 01234567890.,- ) that affect the center-widget's functionality. Some of the property widgets contain line edits.

    Requirements:

    When the user has a line edit focused and types in a line edit, all typed characters should go there. When the focus is anywhere else (pushbutton, ...), the center widget should get these keyPress and keyRelease events and handle them accordingly.

    Implementation-wise this appears to be not so trivial: for one, the center widget does not always have focus and thus does not even receive keyPress/keyRelease-events.
    Also, there are really quite a lot of property widgets, and installing an eventfilter on each of them feels kind of too complicated (for Qt based code, anyway).

    Adding top-level actions and treating the special characters as shortcuts would probably work, but again feels like a hack.

    So, how would you implement this?

    Thanks,
    Andreas
    Andreas

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Global key press handling: best practice?

    You might look at deriving your own application class from somewhere in the QCoreApplication hierarchy and overriding the QCoreApplication::notify() method, or using the standard application class and installing an event filter on it. All events application wide go through either the notify() function or the event filter if installed.

    Whichever you choose, you can examine the object and event types and choose to let the event get handled as normal or to redirect it to the central widget. (Although I am not sure how redirecting a key press on a button could be reinterpreted in a widget of a different type).

    You don't need to install an event filter on every widget; doing it at the application level (through notify() or an app-level filter) means you'll get to see every event before any other QObject does.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Cruz (19th March 2021)

  4. #3
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    100
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Global key press handling: best practice?

    Thanks! I had also thought of doing this application wide - I still need to check the focusedWidget() type - if it is indeed a line edit, I'll let the keys pass through.
    Andreas

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

    Cruz (26th March 2021)

  6. #4
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    100
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: Global key press handling: best practice?

    Btw, just to close this topic off. What I successfully did was:

    - subclass QApplication and re-implement notify
    - in there check for keypress event
    - if received, check if sender was a line edit -> if so, ignore it
    - otherwise handle as global key press

    Qt Code:
    1. bool SVDebugApplication::notify( QObject *recv, QEvent *e ) {
    2. try {
    3. if (e->type() == QEvent::KeyPress) {
    4. // got a keypress event
    5. QWidget * w = focusWidget();
    6. // was it sent from a line edit?
    7. if (qobject_cast<QLineEdit*>(w) == nullptr) {
    8. // no, handle as global key press
    9. QKeyEvent * ke = dynamic_cast<QKeyEvent *>(e);
    10. if (SVViewStateHandler::instance().m_geometryView->handleGlobalKeyPress(ke))
    11. return true;
    12. }
    13. }
    14. }
    15. return QApplication::notify( recv, e );
    16. }
    17. catch (Vic3D::Exception &ex) {
    18. ex.writeMsgStackToError();
    19. IBK::IBK_Message("Vic3D::Exception caught.", IBK::MSG_ERROR, FUNC_ID);
    20. }
    21.  
    22. return false;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Andreas

Similar Threads

  1. catch global mouse press event
    By nick85 in forum Newbie
    Replies: 3
    Last Post: 15th June 2011, 13:37
  2. Key Press Event Handling
    By kosasker in forum Newbie
    Replies: 8
    Last Post: 22nd March 2011, 15:49
  3. Touchscreen Best Practice ?
    By vendra in forum Newbie
    Replies: 1
    Last Post: 11th January 2011, 12:43
  4. Best practice when handling a slow loading widget?
    By TheNewGuy in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2009, 06:12
  5. what's the better practice?
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 18:42

Tags for this Thread

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.