I would like to handle keyboard events in command line application based on QCoreApplication class. Following http://doc.qt.nokia.com/4.7/eventsandfilters.html I tried to reimplementing QCoreApplication::event(), but my code not works:
#include <QCoreApplication>
#include <QtGui/QKeyEvent>
class CoreApp : public QCoreApplication
{
Q_OBJECT
public:
explicit CoreApp(int & argc, char ** argv);
bool event(QEvent *event);
};
CoreApp::CoreApp(int & argc, char ** argv) :
QCoreApplication(argc,argv)
{
}
bool CoreApp::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Q) {
qDebug("Quit?");
//qApp->quit();
return true;
}
}
return QCoreApplication::event(event);
}
int main(int argc, char *argv[])
{
CoreApp a(argc, argv);
return a.exec();
}
I've checked http://doc.qt.nokia.com/qq/qq11-events.html and didn't find solution. How to handle these events properly?