Why key press events of some keys are not sent to notify()



  • I am working on a Qt gui application that defines its own application class inheriting from QApplication and overrides nofity() method. I found key press events of Ctrl + L and some other keys are not sent to this method and key release events work well, but everything works correctly if I create a clean application with Qt Creator. I want to find out the root cause of this problem, but I do not have any idea about how to make it.
    Does Qt has any mechanism to filter event out before sending to notify()? Or any solution to find out the root cause?


  • Moderators

    @xiazeyi Are you sure Ctrl-L is not sent? Can you show the notify() method?



  • @jsulm With the following code, only key release event is printed for Ctrl+L

    bool MyQtApplication::notify(QObject* receiver, QEvent* event)
    {
        try
        { 
            if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
            {
                auto keyEvent = dynamic_cast<QKeyEvent*>(event);
                std::cout << "type: " << keyEvent->type() << "; modifiers: " << keyEvent->modifiers() << "; key: " << keyEvent->key() << std::endl;
            }
            return QApplication::notify(receiver, event);
        }
        catch(const std::exception& ex)
        { 
            std::cout << "unhandled exception: " << ex.what() << std::endl;
        }
        catch(...)
        { 
            std::cout << "unhandled exception occured" << std::endl;
        }
        
        return false;
    }
    


  • @xiazeyi
    Verify whether you are receiving a QEvent::ShortcutOverride instead of QEvent::KeyPress? Also see https://forum.qt.io/topic/34583/solved-qwidget-eventfilter-not-catching-key-combinations



  • @JNBarchan It works. So it means Ctrl+L is defined as a shortcut in the application, and according to this clue I have found the code to set Ctrl+L as shortcut of an action.
    Thanks!



  • @xiazeyi
    Yeah, I know, I'm a genius... ;-) I've never even seen a keypress event....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.