# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class MyLineEdit(QtGui.QLineEdit):
def focusOutEvent(self, event):
print "focus out"
super(MyLineEdit, self).focusOutEvent(event)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
a = MyLineEdit(self)
b = MyLineEdit(self)
a.move(60, 100)
b.move(60, 50)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Hello!')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
или так
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def p(self):
print text
def initUI(self):
self.a = QtGui.QLineEdit(self)
self.a.installEventFilter(self)
self.b = QtGui.QLineEdit(self)
self.b.installEventFilter(self)
self.a.move(60, 100)
self.b.move(60, 50)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Hello!')
self.show()
def eventFilter(self, obj, event):
if obj == self.b:
if event.type() == QtCore.QEvent.FocusOut:
print("Focus Out")
return False
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()