'Catch clicks outside the object and ESC key press.'

def eventFilter(self, widget, event):

    if (((event.type() == QEvent.MouseButtonPress) and (not self.geometry().contains(event.globalPos()))) or ((event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Escape))):
        self.hide()
        self.setFocus(False)
        return True
    return QLineEdit.eventFilter(self, widget, event)
