Qt每当按下进入QTextEdit

时间:2013-05-02 13:39:10

标签: c++ qt events qtextedit

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

1 个答案:

答案 0 :(得分:3)

您需要继承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);
        }
    }
};

或者,您可以在文本编辑上install an event filter