Isn't it easier to switch the keyboard layout on focusInEvent and switch it back on focusOutEvent?