3

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_()
BenjaminK
  • 653
  • 1
  • 9
  • 26
  • please provide a [mre] – eyllanesc Jun 07 '20 at 00:30
  • @eyllanesc What else do you wish for? I already provided the most important code? – BenjaminK Jun 07 '20 at 00:32
  • Here we do not need *the most important code* according to your point of view, here the MRE is required. – eyllanesc Jun 07 '20 at 00:38
  • What is the MRE? I don't know the exact method that needs to be called, that's my problem. – BenjaminK Jun 07 '20 at 00:41
  • What I need is a code that I can test, please read the link that I gave you – eyllanesc Jun 07 '20 at 00:43
  • I can't get the application to run as a simple example. My own example is 800 lines of code split into three files. And at the moment simplifying it to a simple example doesn't run. But I can post my code so far. But when simplifying it I got other problems like for example that the LineEdit won't show up.. – BenjaminK Jun 07 '20 at 01:03
  • That's what I'm trying to tell you. I can't get the MRE code to run as to make an MRE I have to find out how to run a program in a simple way without splitting it into multiple files. I'm sorry but as said I need help with this. – BenjaminK Jun 07 '20 at 01:07
  • @eyllanesc Now you can copy the example and it should work. I hope that helps. Thank you in advance. – BenjaminK Jun 07 '20 at 01:20

1 Answers1

3

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_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you so much for your answer! I wouldn't be able to come up with the construction myself, so it's really helpful for me to understand the syntax of the EventFilter. Would you mind adding a link to any resource where you got that information from? Why do you return the `super(QTApp, self).eventFilter(obj, event)`. Also, do event filters make applications slow as they constantly checking? – BenjaminK Jun 07 '20 at 13:56
  • 1
    @BenjaminK 1) I already added the docs, 2) A filter not only serves to listen but also to reject events, so a boolean must be returned (read the docs for more information), and to not remove the default behavior I return the same as returns the parent class. 3) An "if" is not time consuming so it should not slow down anything, if that happens to you then the error is probably in another part of your code, the codes in the GUI should not be very time consuming, and if there are tasks time consuming then they must be run on another thread. – eyllanesc Jun 07 '20 at 14:05
  • As the documentation is in C++ it's hard to read for somebody like me who has a basic understanding of python. Events ins this example seem to be an advanced topic, but very interesting. Thanks for trying to make it more intuitive for me. – BenjaminK Jun 07 '20 at 14:26
  • 1
    @BenjaminK Consider it as a pseudocode, and only docs it (the text) since it is very accurate – eyllanesc Jun 07 '20 at 14:28