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

本文介绍了解决Qt中OpenGL程序键盘事件未响应的问题。通过设置QGLWidget子类的焦点策略为StrongFocus,使得键盘事件能够被正确处理。

      最近在调试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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值