如何连接QLineEdit的focusOutEvent
我用PyQt4的Designer设计了一个窗口,里面有一个QLineEdit
(就是一个可以输入文本的框)。我把.ui
文件转换成了.py
文件,使用的是pyuic4
这个工具。然后我又创建了一个新的.py
文件,并且导入了之前生成的Ui_Class
,还对它进行了子类化。
我想在QLineEdit
失去焦点的时候执行一些操作。
就像按钮被点击时会触发事件一样,我也想把QLineEdit
失去焦点的事件连接起来。
1 个回答
10
使用一个 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)