Event leave on QLineEdit

Viewed 261

I want to do some action if the user typed text into the QLineEdit and left the QLineEdit entry field.

There is not a lot of explanation about focusInEvent available. I'm using Python and PySide2. But all information is helping.

def check_focus(self, event):
    print('Focus in event')
    # Way one I tried
    if QtGui.QFocusEvent.lostFocus(self.LE_sample_input_01):
         if self.LE_sample_input_01.text():
             print("Lost focus") 

    # Way two I tried
    if event.type() == QtCore.QEvent.FocusIn:
        if self.LE_sample_input_01.text():
            print(f"Lost focus")

    # Way three I tried
    if self.LE_sample_input_01.focusInEvent(event)

Simple example with two QLineEdits

So it's possible to leave one entry

from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.QtGui import QIcon


class QTApp(QtWidgets.QWidget):
    def __init__(self):
        super(QTApp, self).__init__()

        self.LE_sample_input_01 = QtWidgets.QLineEdit()
        self.LE_sample_input_02 = QtWidgets.QLineEdit()

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.LE_sample_input_01)
        layout.addWidget(self.LE_sample_input_02)
        self.setLayout(layout)

    def check_focus(self, event):
        print('focus out event')
        # if QtGui.QFocusEvent.lostFocus(self.LE_client_name):
        #     print("lost focus") self.LE_client_name.focusInEvent(event)
        if event.type() == QtCore.QEvent.FocusIn:
            if self.LE_sample_input_01.text():
                print(f"Lost focus")

if __name__ == '__main__':
    app = QtWidgets.QApplication()
    qt_app = QTApp()
    qt_app.show()
    app.exec_()
1 Answers

The check_focus method doesn't exist so it obviously won't work. If you want to listen to events from another QObject like QWidet then you should use an eventFilter:

from PySide2 import QtWidgets, QtCore, QtGui


class QTApp(QtWidgets.QWidget):
    def __init__(self):
        super(QTApp, self).__init__()

        self.LE_sample_input_01 = QtWidgets.QLineEdit()
        self.LE_sample_input_02 = QtWidgets.QLineEdit()

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.LE_sample_input_01)
        layout.addWidget(self.LE_sample_input_02)

        self.LE_sample_input_01.installEventFilter(self)
        self.LE_sample_input_02.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.FocusIn:
            print("FocusIn")
            if obj is self.LE_sample_input_01:
                print("LE_sample_input_01")
            elif obj is self.LE_sample_input_02:
                print("LE_sample_input_02")
        elif event.type() == QtCore.QEvent.FocusOut:
            print("FocusOut")
            if obj is self.LE_sample_input_01:
                print("LE_sample_input_01")
            elif obj is self.LE_sample_input_02:
                print("LE_sample_input_02")
        return super(QTApp, self).eventFilter(obj, event)


if __name__ == "__main__":
    app = QtWidgets.QApplication()
    qt_app = QTApp()
    qt_app.show()
    app.exec_()
Related