Newbee: eventFilter on QCoreApplication... how?
-
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.
MarcWindows 8.1, Visual Studio 2015 Community (update3), Qt5.7 => x64/Debug
-
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.
MarcWindows 8.1, Visual Studio 2015 Community (update3), Qt5.7 => x64/Debug
@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.