为什么PyQt中的keyPress事件不能用于密钥Enter?

Ily*_*nko 5 events keypress pyqt4 python-3.x

为什么,当我按下时Enter,该keyPressEvent方法不能满足我的需要?它只是将光标移动到一个新行.

class TextArea(QTextEdit):
    def __init__(self, parent):
        super().__init__(parent=parent)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.show()

    def SLOT_SendMsg(self):
        return lambda: self.get_and_send()

    def get_and_send(self):
        text = self.toPlainText()
        self.clear()
        get_connect(text)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Enter: 
            self.get_and_send()
        else:
            super().keyPressEvent(event)
Run Code Online (Sandbox Code Playgroud)

war*_*iuc 7

Qt.Key_Enter 是位于键盘上的Enter:

Qt::Key_Return  0x01000004   
Qt::Key_Enter   0x01000005  Typically located on the keypad.
Run Code Online (Sandbox Code Playgroud)

使用:

def keyPressEvent(self, qKeyEvent):
    print(qKeyEvent.key())
    if qKeyEvent.key() == QtCore.Qt.Key_Return: 
        print('Enter pressed')
    else:
        super().keyPressEvent(qKeyEvent)
Run Code Online (Sandbox Code Playgroud)