How to remove focus from a QLineEdit when anyplace

2019-04-21 10:22发布

问题:

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?

回答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.



回答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.



回答3:

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

QApplication.focusWidget().clearFocus()

would fix the problem.



回答4:

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



标签: qt qt4 focus