Search code examples
qtqt4focus

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


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?


Solution

  • 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.