需要接收键盘事件,但是又不能有界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
}

1、首先需要在main方法中注册,使用installEventFilter方法把这个类的指针传进去

2、在Test类中重写eventFilter方法,这样就可以进行监听了

3、在eventFilter中进行自己的逻辑处理

摘录自:https://www.cnblogs.com/xupeidong/p/11152998.html