Search code examples
c++qtqt-designerqtextedit

How to override keyPressEvent of QTextEdit?


I override keyPressEven() of widget QTextEdit:

void myTextEdit::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_0)
    {
        qDebug() << "Ok";
    }
}

Button 0 works - show "Ok", but does not write in field of QTextEdit. Why? Thanks advance.


Solution

  • You need to call the base class implementation if you want to keep the default behaviour:

    void myTextEdit::keyPressEvent(QKeyEvent *e)
    {
        if(e->key()==Qt::Key_0)
        {
            qDebug() << "Ok";
        }
        QTextEdit::keyPressEvent(e);
    }
    

    See the docs for keyPressEvent.