Qt 4.6 和 OpenGL:如何同时使用三个不同的小部件捕获键盘按下?
我在 Qt Creator 中创建了一个表单,并在表单中添加了三个自定义 QWidget(都是同一个类,称为 Renderer)。我希望当用户按下 Alt 键时,除了表单之外的所有三个小部件都能收到通知,但目前我什至无法让其中一个小部件工作。
我将 void keyPressEvent(QKeyEvent *) 和 void keyReleaseEvent(QKeyEvent *) 添加到 Renderer 类中,但是根本没有调用这些函数...(没有触发断点)按键函数受到保护。
void Renderer::keyPressEvent(QKeyEvent *event) {
switch(event->key()) {
case Qt::Key_Alt: {
isAltPressed = true;
cout << "alt got pressed" << endl;
break;
}
default:
break;
}
}
void Renderer::keyReleaseEvent(QKeyEvent *event) {
switch(event->key()) {
case Qt::Key_Alt: {
isAltPressed = false;
cout << "alt released" << endl;
break;
}
default:
break;
}
}
我在这里错过了什么吗?
另外,为了让所有三个小部件同时注册键盘按下,我需要做什么特别的事情吗?
多谢。
I created a form in Qt Creator, and added three custom QWidgets (all are the same class called Renderer) into the form. I want all of the three widgets besides the form to get notified when a user presses the Alt key, but I cannot even get one to work at the moment.
I added the void keyPressEvent(QKeyEvent *) and void keyReleaseEvent(QKeyEvent *) into the Renderer class, but the functions are not called at all... (breakpoints were not triggered) The key press functions are protected.
void Renderer::keyPressEvent(QKeyEvent *event) {
switch(event->key()) {
case Qt::Key_Alt: {
isAltPressed = true;
cout << "alt got pressed" << endl;
break;
}
default:
break;
}
}
void Renderer::keyReleaseEvent(QKeyEvent *event) {
switch(event->key()) {
case Qt::Key_Alt: {
isAltPressed = false;
cout << "alt released" << endl;
break;
}
default:
break;
}
}
Am I missing something here?
Also, is there anything special I have to do in order to have keyboard presses registered by all three widgets at the same time?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的
focusPolicy
是什么?键盘事件只会发送到具有焦点的小部件(然后您可以自己调用其他处理程序)。What's your
focusPolicy
? Keyboard events will only go to the widget with focus (you can then call the other handlers yourself).