Qt 4.6 和 OpenGL:如何同时使用三个不同的小部件捕获键盘按下?

发布于 2024-10-04 08:54:12 字数 847 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-10-11 08:54:12

你的focusPolicy是什么?键盘事件只会发送到具有焦点的小部件(然后您可以自己调用其他处理程序)。

What's your focusPolicy? Keyboard events will only go to the widget with focus (you can then call the other handlers yourself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文