Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QKeyEvent on whole applicacation [solved]

    General and Desktop
    2
    4
    1293
    Loading More Posts
    • 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
      jarosda last edited by

      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 Reply Quote 0
      • A
        akonradwesolutions last edited by

        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 Reply Quote 0
        • J
          jarosda last edited by

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

          1 Reply Last reply Reply Quote 0
          • A
            akonradwesolutions last edited by

            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 Reply Quote 0
            • First post
              Last post