Search code examples
qt

How to focus on a next/previous widget


I am overriding the QPlainTextEdit to become a single line edit widget. In other words I want it to look like a QLineEdit but to have the extended functionality of QPlainTextEdit, such as text formatting etc.

My only trouble so far is that I do not know how to pass focus to the next/previous widget when I press Tab/Shift+Tab when the widget derived from QPlainTextEdit is in focus. I have started with overriding keyPressEvent to capture Tab key being pressed, but then what? How to change the focus from the widget?

I can only think of too complicated solutions (such as signalling to the parent that the focus should change, but this seems as stupid overkill). I bet there must be a very simple solution to this problem.


Solution

  • Set the tabChangesFocus property of your QPlainTextEdit object to true by using the QPlainTextEdit::setTabChangesFocus function.