您需要QTextEdit通过覆盖适当的方法来子类化并捕获您感兴趣的事件:
class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
void MyTextEdit::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
login(); // or rather emit submitted() or something along this way
}
else
{
QTextEdit::keyPressEvent(event);
}
}
};
Run Code Online (Sandbox Code Playgroud)