Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to switch focus without losing preedit
Forum Update on Monday, May 27th 2025

How to switch focus without losing preedit

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 85 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Isopod
    wrote on last edited by
    #1

    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 forwarding QEvent::InputMethod in addition to QEvent::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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved