confusedKid
confusedKid

Reputation: 3251

Qt 4.6 and OpenGL: how to capture keyboard presses with three different widgets at once?

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.

Upvotes: 2

Views: 3261

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283664

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

Upvotes: 3

Related Questions