Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QLineEdit上的事件假期_Python_Pyside2 - Fatal编程技术网

Python QLineEdit上的事件假期

Python QLineEdit上的事件假期,python,pyside2,Python,Pyside2,如果用户在QLineEdit中键入文本并离开QLineEdit输入字段,我想执行一些操作 关于focusInEvent的解释不多。我正在使用Python和PySide2。但所有信息都有帮助 def check_focus(self, event): print('Focus in event') # Way one I tried if QtGui.QFocusEvent.lostFocus(self.LE_sample_input_01): if se

如果用户在QLineEdit中键入文本并离开QLineEdit输入字段,我想执行一些操作

关于
focusInEvent
的解释不多。我正在使用Python和PySide2。但所有信息都有帮助

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)
带有两个QLineEdits的简单示例 所以可以留下一个条目

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_()

check_focus方法不存在,因此显然不起作用。如果您想收听来自其他QObject(如QWidet)的事件,则应使用:


请提供一个@eyllanesc,您还想要什么?我已经提供了最重要的代码?这里我们不需要最重要的代码根据你的观点,这里需要MRE。什么是MRE?我不知道需要调用的确切方法,这是我的问题。我需要的是一个可以测试的代码,请阅读我给你的链接谢谢你的回答!我自己无法提出构造,因此理解EventFilter的语法对我来说非常有帮助。您介意添加一个链接到您从中获取信息的任何资源吗?为什么返回
super(QTApp,self).eventFilter(obj,event)
。此外,事件过滤器是否会使应用程序在不断检查时变慢?@BenjaminK 1)我已经添加了文档,2)过滤器不仅用于侦听事件,还用于拒绝事件,因此必须返回布尔值(阅读文档了解更多信息),为了不删除默认行为,我返回的结果与返回父类的结果相同。3) 一个“如果”并不耗时,因此它不应该减慢任何速度,如果发生这种情况,那么错误可能在代码的另一部分,GUI中的代码不应该非常耗时,如果任务耗时,那么它们必须在另一个线程上运行。因为C++中的文档,对于像我这样对Python有基本理解的人来说,很难阅读。本例中的事件似乎是一个高级主题,但非常有趣。谢谢你让我觉得它更直观。@本杰明认为它是伪代码,只记录它(文本),因为它非常精确。
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_()