PyQt里的EventFilter

构造函数添加:

self.edit.installEventFilter(self)
    def eventFilter(self, obj, event):
        if obj == self.edit:
            if event.type() == QEvent.KeyPress and (event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return):
                pass
                return False
            elif event.type() == QEvent.KeyPress and event.key() == Qt.Key_Backspace:
                getEditStr = self.edit.toPlainText()
                if len(getEditStr) != 0:
                    if self.edit.toPlainText()[-1] == '\n':
                        pass
                return False
            else:
                return False
        else:
            return QWidget.eventFilter(obj, event)


你可能感兴趣的:(Qt,Python)