How to remove focus from a QLineEdit when anyplace else on the window is clicked

26,393

Solution 1

I've found a solution that seems to work, though I'm still open to other options if there are any. I'm using PyQt4, so my example is in python:

Create a subclass of QLineEdit just so I have a new type. I don't want or need this behavior on all QLineEdit instances; just these specific ones.

class MyLineEdit(QtGui.QLineEdit):
    pass

Now, in my QMainWindow subclass, I override the mousePressEvent() implementation. It gets the currently focused widget. If that widget is of type MyLineEdit, clear the focus.

class MyMainWindow(QtGui.QMainWindow):
    def ...

    def mousePressEvent(self, event):
        focused_widget = QtGui.QApplication.focusWidget()
        if isinstance(focused_widget, MyLineEdit):
            focused_widget.clearFocus()
        QtGui.QMainWindow.mousePressEvent(self, event)

    def ...

This gets me the behavior I'm looking for so that if the user clicks anywhere on the application's window, the focus is cleared.


Edit: I did find one caveat to this. I have a QTreeView in the main window. If the user clicks on the tree view, focus is not removed from the text edit field.

Solution 2

Catch the clicked() signal of your parent widget and call yourLabel->clearFocus() (that unfortunatelly happens to not be a slot, making things more complicated) there.

Solution 3

I followed Grant Limberg instruction here but figured out that, in my case, a simple:

QApplication.focusWidget().clearFocus()

would fix the problem.

Solution 4

I'm not sure if this also works in Qt4 (I'm using PyQt5) but you can change the FocusPolicy of the QMainWindow or parent widget to clear the focus in the QLineEdit. As per https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

I've changed the policy of my QMainWindow to Qt.StrongFocus and it worked like the functionality described in the question.

Solution 5

If done in C++ I would do something along the lines of:

connect(myWidgets->MyLineEdit, SIGNAL(returnPressed()), this, SLOT(onLineEditDone());

void onLineEditDone()
{
    myWidgets->MyLineEdit->clearFocus();
}

For this particular case I would use editingFinished() instead of returnPressed(), probably, but I would NOT use textChanged(QString).

Share:
26,393
Grant Limberg
Author by

Grant Limberg

Sr Software Engineer and Systems Administrator @ZeroTier

Updated on November 19, 2020

Comments

  • Grant Limberg
    Grant Limberg over 3 years

    I'm working on a custom Qt button that allows you to edit the text on the button if you double click it. When the button is double clicked, a QLineEdit appears where the text on the button is allowing the user to edit the text on the button. My requirement is that if the user clicks anywhere in the application window, the QLineEdit should disappear and cancel the edit operation. This works in some cases. Specifically, it works if I click on anything that is capable of text entry. Other portions of the window don't work as expected. I'll click on a blank portion of the application window, and the QLineEdit retains its focus. How can I remove its focus in these cases?