Unsolved How to switch focus without losing preedit
-
Hi. Let's say I have the following requirements:
- a widget (e.g. treeview)
- a QLineEdit
- when the treeview is focused and a key is typed, the QLineEdit is automatically focused and all input is redirected to it
It is easy to achieve this for western input by doing something like this:
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { QApplication *app = static_cast<QApplication*>(QApplication::instance()); if (obj == ui->treeview && event->type() == QEvent::KeyPress) { ui->lineEdit->setFocus(); app->sendEvent(ui->lineEdit, event); return true; } return false; }
The problem is, this doesn't work when an IME is used, since no
QEvent::KeyPress
events are generated in that case. I've tried forwardingQEvent::InputMethod
in addition toQEvent::KeyPress
, but that leads to more problems:QEvent::InputMethod
is emitted as soon as a widget receives focus, even if nothing has been typed yet.- As a workaround, I could only redirect if the preedit is not empty, but then the IME is reset by
ui->lineEdit->setFocus
and the preedit is lost
How can I set focus to a widget without resetting the IME in Qt5? I thought this would be a common usecase, I'm surprised it is so difficult.