如何连接QLineEdit focusOutEvent

时间:2013-02-25 12:23:25

标签: python pyqt4 qlineedit

我在Designer的帮助下在PyQt4中设计了一个带QLineEdit的窗口。我使用.ui.py转换为pyuic4。我创建了另一个.py文件并导入和子类化了Ui_Class

我希望在QLineEdit失去焦点时执行一些任务。

点击线按钮点击事件i以连接QLineEdit丢失焦点事件

1 个答案:

答案 0 :(得分:7)

使用eventFilter

class Filter(QtCore.QObject):
    def eventFilter(self, widget, event):
        # FocusOut event
        if event.type() == QtCore.QEvent.FocusOut:
            # do custom stuff
            print 'focus out'
            # return False so that the widget will also handle the event
            # otherwise it won't focus out
            return False
        else:
            # we don't care about other events
            return False

在你的窗口中:

# ...
self._filter = Filter()
# adjust for your QLineEdit
self.ui.lineEdit.installEventFilter(self._filter)