Question:最近在搞linux下的一個客戶端項目,須要接收鍵盤事件,可是又不能有界面,這種狀況怎麼處理呢?linux
int main(int argc, char *argv[]) { QApplication a(argc, argv); Test *p = new Test; a.installEventFilter(p); return a.exec(); } bool Test::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::KeyPress) { static int index = 0; QKeyEvent *key=static_cast<QKeyEvent *>(event); ......... } return QObject::eventFilter(obj,event); }
一、首先須要在main方法中註冊,使用installEventFilter方法把這個類的指針傳進去spa
二、在Test類中重寫eventFilter方法,這樣就能夠進行監聽了指針
三、在eventFilter中進行本身的邏輯處理code