Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
Normal People Scare Me Avatar asked Feb 17 '23 15:02

Normal People Scare Me


1 Answers

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.

like image 68
Luc Touraille Avatar answered Feb 28 '23 04:02

Luc Touraille