2013-02-25 36 views
1

我在Designer的幫助下,在PyQt4中設計了一個帶有QLineEdit的窗口。我使用pyuic4.ui轉換爲.py。我創建了另一個.py文件,並導入了子文件Ui_Class如何連接QLineEdit focusOutEvent

我想在QLineEdit失去焦點時執行一些任務。

就行按鈕點擊事件我連接QLineEdit失去焦點事件

回答

6

使用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)