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. Newbee: eventFilter on QCoreApplication... how?
Forum Updated to NodeBB v4.3 + New Features

Newbee: eventFilter on QCoreApplication... how?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.1k Views 2 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.
  • O Offline
    O Offline
    obiwanjacobi
    wrote on last edited by
    #1

    Hi,

    I'm very new to Qt and am trying to write a console application that will intercept key strokes (when it has the focus) and send these over USB to an external hardware device.

    After reading some of the docs I tried to implement an eventFilter and add that to the application using installEventFilter.

    class KeyEventFilter : public QObject {
    	Q_OBJECT
    
    public:
    	KeyEventFilter(QObject * parent = Q_NULLPTR);
    	~KeyEventFilter();
    
    	bool eventFilter(QObject* o, QEvent* e) override;
    };
    
    bool KeyEventFilter::eventFilter(QObject* o, QEvent* e)
    {
    	qDebug() << "Type:" << e->type();
    
    	if (e->type() == QEvent::KeyPress) {
    		QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
    		qDebug() << "Key:" << keyEvent->key();
    	}
    
    	return QObject::eventFilter(o, e);
    }
    
    int main(int argc, char *argv[])
    {
    	QCoreApplication app(argc, argv);
    
    	auto filter = new KeyEventFilter();
    	app.installEventFilter(filter);
    
    	return app.exec();
    }
    
    

    qDebug() traces or any breakpoints do not seem to get called.

    The I have tried to derive from QCoreApplication and override the notify or eventFilter methods.

    class Program : public QCoreApplication
    {
    public:
    	Program(int argc, char *argv[]) : QCoreApplication(argc, argv) {}
    
    	bool notify(QObject* o, QEvent* e) override {
    		qDebug() << "Type:" << e->type();
    		return QCoreApplication::notify(o, e);
    	}
    
    	bool eventFilter(QObject* o, QEvent* e) override {
    		qDebug() << "Type:" << e->type();
    		return QCoreApplication::eventFilter(o, e);
    	}
    };
    

    These methods also do not get called. I suspect there may be a fundamental problem but I can't see what it could be.
    Marc

    Windows 8.1, Visual Studio 2015 Community (update3), Qt5.7 => x64/Debug

    jsulmJ 1 Reply Last reply
    0
    • O obiwanjacobi

      Hi,

      I'm very new to Qt and am trying to write a console application that will intercept key strokes (when it has the focus) and send these over USB to an external hardware device.

      After reading some of the docs I tried to implement an eventFilter and add that to the application using installEventFilter.

      class KeyEventFilter : public QObject {
      	Q_OBJECT
      
      public:
      	KeyEventFilter(QObject * parent = Q_NULLPTR);
      	~KeyEventFilter();
      
      	bool eventFilter(QObject* o, QEvent* e) override;
      };
      
      bool KeyEventFilter::eventFilter(QObject* o, QEvent* e)
      {
      	qDebug() << "Type:" << e->type();
      
      	if (e->type() == QEvent::KeyPress) {
      		QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
      		qDebug() << "Key:" << keyEvent->key();
      	}
      
      	return QObject::eventFilter(o, e);
      }
      
      int main(int argc, char *argv[])
      {
      	QCoreApplication app(argc, argv);
      
      	auto filter = new KeyEventFilter();
      	app.installEventFilter(filter);
      
      	return app.exec();
      }
      
      

      qDebug() traces or any breakpoints do not seem to get called.

      The I have tried to derive from QCoreApplication and override the notify or eventFilter methods.

      class Program : public QCoreApplication
      {
      public:
      	Program(int argc, char *argv[]) : QCoreApplication(argc, argv) {}
      
      	bool notify(QObject* o, QEvent* e) override {
      		qDebug() << "Type:" << e->type();
      		return QCoreApplication::notify(o, e);
      	}
      
      	bool eventFilter(QObject* o, QEvent* e) override {
      		qDebug() << "Type:" << e->type();
      		return QCoreApplication::eventFilter(o, e);
      	}
      };
      

      These methods also do not get called. I suspect there may be a fundamental problem but I can't see what it could be.
      Marc

      Windows 8.1, Visual Studio 2015 Community (update3), Qt5.7 => x64/Debug

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @obiwanjacobi If it is a console application then you need to use std::cin. You will not have any key-events in a pure console application, those are for UI widgets.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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