Important: Please read the Qt Code of Conduct -

GrabKeyboard troubles

  • I have a main window with a big OpenGL widget that I manipulate 3D objects in. Some of this manipulation is done with keyboard presses so I grab the keyboard in enterEvent and release it in the leaveEvent. This is fine but I also have floating tool bars and dialog boxes that pop up over the big widget. None of the controls (QTextEdit and QSpinBox widgets) get any keyboard input any more - presumably because the mouse is still within the OpenGL widget and so no leave event has been generated. Is there another event I should be trapping to release the keyboard to get all these controls to work as expected when widgets overlap? Perhaps I could do it by trapping QEvent::CursorChange but that seems a bit of an ugly hack. QEvent::WindowBlocked should work for for the dialogs.


  • I've got around this by subclassing the QTextEdit and QSpinBox classes so they send enter and leave signals which I can use to grab and ungrab the keyboard. It still isn't at all neat, and not 100% reliable which surprises me. Am I always guaranteed that each enter event will be paired by a leave event?

Log in to reply