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. Key Event Filter for application
Forum Updated to NodeBB v4.3 + New Features

Key Event Filter for application

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.0k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    gogoer
    wrote on last edited by
    #1

    Hello.
    I have a big apllication with a lot of widgets. And i have a task to filter all autorepeate key press.
    I registerered eventFilter() for application:

    qApp->installEventFilter(this);
    

    but it doest work. it filters events when they ignored by other widgets.
    how can i filter key events before they riches cild wodgets?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Not a good ideal to place the filter for qApp itself. What the is done in the filter method ? Can you show it ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gogoer
        wrote on last edited by
        #3

        i need to filter repeatedly pressed buttons. I tryed do it like this:

        bool ApplicationController::eventFilter(QObject *obj, QEvent *event)
        {
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                int key = keyEvent->key();
                if(key==Qtv::Key_Ok && keyEvent->isAutoRepeat()){
                    return true;
                }
            }
            return false;
        }
        

        but it filters only if other widgets ignores. i need to filter before other widgets.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          You can do it with notify override

          #include <QApplication>
          #include <QKeyEvent>
          class Application final : public QApplication
          {
          public:
              Application(int &argc, char **argv) : QApplication(argc, argv) {}
              virtual bool notify(QObject *receiver, QEvent *event) override
              {
                  if (event->type() == QEvent::KeyPress) {
                      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                      int key = keyEvent->key();
                      if ( keyEvent->isAutoRepeat()) {
                          return true;
                      }
                  }
                 return QApplication::notify(receiver,event);
              }
          };
          

          Credits to dude on stackoverflow.

          1 Reply Last reply
          1

          • Login

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