QGLWidget中keyPressEvent函数没有响应键盘事件的解决方法

      最近在调试Qt的OpenGL程序,继承了QGLWidget并实现了一个新的类,在新类中重新定义了keyPressEvent(QKeyEvent *event)函数,但是程序运行中keyPressEvent函数并没有响应键盘事件,经过上网搜寻在页面http://www.qtcentre.org/threads/22154-QGLWidget-keyPressEvent-does-not-work中找到了解决方法:

     该页面中有以下原因叙述:

"The policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing, Qt::ClickFocus if the widget accepts focus by clicking, Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all."
And
"You must enable keyboard focus for a widget if it processes keyboard events. This is normally done from the widget's constructor. For instance, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus)."

    解决方法是:在继承的类中添加函数:setFocusPolicy(Qt::StrongFocus);

原文地址:https://www.cnblogs.com/ppffs/p/3154163.html