Normal People Scare Me
Normal People Scare Me

Reputation: 841

Qt Whenever pressed enter in QTextEdit

Whenever I pressed enter in my QTextEdit it'll perform a click on my login button. Somehow this causes a crash of my QtCreator. How can I change what'll happen If I press enter in my QTextEdit?

Upvotes: 5

Views: 8601

Answers (1)

Luc Touraille
Luc Touraille

Reputation: 82031

You need to subclass QTextEdit and catch the event you're interested in by overriding the appropriate method:

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);
        }
    }
};

Alternatively, you can install an event filter on the text edit.

Upvotes: 10

Related Questions