Qt Whenever pressed enter in QTextEdit
Asked Answered
C

1

5

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?

Cultigen answered 2/5, 2013 at 13:39 Comment(0)
M
10

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.

Moist answered 2/5, 2013 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.