Question

My complex GUI consists of many native and custom widgets. Most of them has no focus at all (i don't need it: for example, some display-only widgets that doesn't need user's input).

Sometimes QLineEdit appears on the screen when GUI want user to input some string (imagine window's Explorer after pressing F2 having some file selected (rename).

I want user to be able to close this QLineEdit as many ways as possible: pressing Enter, ESC, clicking outside the QLineEdit.

  1. QLineEdit sends editingFinished() signal in some circumstances (press Enter, click on another widget);
  2. Also i can make new class derived from QLineEdit that sends editingFinished() signal when focusOutEvent(QFocusEvent *) occurs.
  3. In that class I can catch ESC key and send the editingFinished().

The only problem is that QLineEdit loses focus only if some another widget takes the focus. So if you click to a widget that doesn't care about focus, the QLineEdit stay having focus and doesn't send the editingFinished() signal.

Was it helpful?

Solution

You will probably need to install an event filter for your entire app, that sends a custom event to all your lineEdits when a mouse click happens. And then in the lineEdit, and implement QObject::event() for your lineEdit class checking for that custom event if it is active or has focus.

I wrote up an answer that points to the documentation for filters like this:

Catching Qt modifier key releases

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top