I’m using QT4 and I created a QGLWidget in which I want to implement a right-click context menu. I want the context menu to appear only when the right-mouse button is clicked and the CTRL key is pressed down.
I tried implementing this by setting a control flag when the CTRL key is pressed, and then clearing it when the CTRL key is released (via a keyReleaseEvent). The problem is that I don’t receive a keyReleaseEvent when the context menu is up (not just CTRL, no key release at all).
For example: I hold down CTRL, click the right mouse button, and then the context menu appears (still holding CTRL). Without selecting anything in the menu, I release the CTRL key, but my program does not receive a keyPressEvent. Therefore my flag never gets cleared, and the next time the user right clicks (even without holding control) the context menu will appear.
Why don’t I receive a keyPressEvent when a context menu is present?
I’m using QT 4.8 (C++) and tested this on both Windows and Linux with the same result.
For the why part, Qt sends key events to the widget which has keyboard focus or is explicitly grabbing the keyboard (with
QWidget::grabKeyBoard()), since the context menu is that widget at the time you release the key, it receives the key release event.