Show with qdebug keyevent



  • Hi i want to show my filter in my main function but i couldn't.

    ```
    

    Código filter.h:
    #ifndef FILTRODEEVENTOSPERSONALIZADO_H
    #define FILTRODEEVENTOSPERSONALIZADO_H

    #include <QObject>
    
    class FiltroDeEventosPersonalizado
      : public QObject
    {
      Q_OBJECT
    
    protected:
    
      bool eventFilter(QObject *obj, QEvent *event) override;
    };
    #endif // FILTRODEEVENTOSPERSONALIZADO_H
    
    
    Código filter.cpp:
    
        ```
    #include "filtrodeeventospersonalizado.h"
        #include <QEvent>
        #include <QDebug>
        #include <QKeyEvent>
        
        bool
        FiltroDeEventosPersonalizado::eventFilter(
            QObject *obj,
            QEvent *event)
        {
          if (event->type() == QEvent::KeyPress) {
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
            if( keyEvent->key() == Qt::Key_A )
             qDebug() << "Tecla 'A' presionada";
            return true;
          }
        
          return QObject::eventFilter(obj, event);
        }
    

    Main:

    ```
    

    #include <QCoreApplication>
    #include <filtrodeeventospersonalizado.h>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    FiltroDeEventosPersonalizado *filtro = new FiltroDeEventosPersonalizado;
    a.installEventFilter(filtro);
    return a.exec();
    }

    Hi i only want to show since qdebug my key pressed A. thx

  • Moderators

    @RIVOPICO
    if thats all the code you are using, then maybe it is caused by the fact that there is no widget/window which would receive the key event.



  • @raven-worx but i can't show the event? with qdebug?


  • Moderators

    @RIVOPICO
    as i said: there is no key event when there is no window to receive it.
    Create one and it should work as expected.


Log in to reply
 

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