最近在写一个控制台程序,需要检测键盘的按键,于是自己从QObject类继承写了个类,重写了bool event(QEvent *e)方法。但是按键时却没有执行。大家帮我看看是我哪里弄错了。
main.cpp:
-------------------------------------
#include <QtCore>
#include <QCoreApplication>
#include "console.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Console console;
return a.exec();
}
--------------------------------------
console.h
--------------------------------------
#ifndef CONSOLE_H_
#define CONSOLE_H_
#include <QObject>
class Console : public QObject
{
Q_OBJECT
public:
Console(QObject *p=0);
bool event(QEvent *e);
};
#endif /*CONSOLE_H_*/
--------------------------------------
console.cpp
--------------------------------------
#include "console.h"
Console::Console(QObject *p)
: QObject(p)
{
}
bool Console::event(QEvent *e)
{
printf("event.\n") ;
return TRUE;
}
--------------------------------------