Qt 每当按下进入 QTextEdit

Nor*_* Me 3 c++ events qt qtextedit

每当我在 QTextEdit 中按下 Enter 键时,它都会点击我的登录按钮。不知何故,这会导致我的 QtCreator 崩溃。如果我在 QTextEdit 中按 Enter,我该如何更改会发生什么?

Luc*_*lle 7

您需要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)

或者,您可以在文本编辑器上安装事件过滤器。