Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QKeyEvent on whole applicacation [solved]
QtWS25 Last Chance

QKeyEvent on whole applicacation [solved]

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jarosda
    wrote on last edited by
    #1

    Hi Guys, I have simple question. How could I catch QKeyEvent in whole application (in all widgets)? In need to proccess key pressing in my application in one place (independently on what child widget is focused on). I know there is eventfiletr but I exactly dont know how to use it. Or if someone could send me link for tutorial about this topic, I'll be happy. Thanks David

    1 Reply Last reply
    0
    • A Offline
      A Offline
      akonradwesolutions
      wrote on last edited by
      #2

      You can subclass QApplication and reimplement QApplication::notify - to handle the events you are interested in globally:

      @
      bool MyApplication::notify(QObject *receiver, QEvent *event)
      {
      if (event->type() == QEvent::KeyPress) {
      // Handle
      }
      return QApplication::notify(receiver, event);
      }
      @

      It should be noted that this is rather intrusive. Be sure to call the base implementation unless you are very convinced that you don't want to deliver the event to the proper receiver.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jarosda
        wrote on last edited by
        #3

        Thanks Arnold, it works correctly. May I ask you if it is standard way how to do it?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          akonradwesolutions
          wrote on last edited by
          #4

          It is one of two different ways to process Qt events globally (at least to my knowledge) using the Qt API. The second way to achieve a similar result is to install an event filter on the QApplication instance you want to monitor. I must admit that I simply forgot to mention the second possibility :). The major advantage of the second way is that you can avoid subclassing QApplication. Which one is actually preferrable depends on the use case (and personal preference of course). I, for my part, do prefer the method I suggested. In my experience problems caused by erroneous event filters are often hard to spot.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved