[SOLVED] textEdit return key was pressed



  • can i have an example please on how to tell if the return key was pressed in the textEdit widget?



  • Subclass QTextEdit, then reimplement the keyPressEvent() or event(). And maybe you can found many such example in http://doc.qt.nokia.com/4.7/all-examples.html

    If you don't want to subclass QTextEdit, you can using eventfilter, example can be found in the manual: http://doc.qt.nokia.com/4.7/qobject.html#installEventFilter



  • can i have examples please because i am trying problems implementing the if return key was pressed in the textedit widget.

    below is the code that is not working. i am trying to get the code to click the saybutton when the return key is clicked in the textedit

    @void MainWindow::keyPressEvent (QKeyEvent *e)
    {
    if ((e->key () == Qt::Key_Enter))
    ui->sayButton->animateClick(1100);

    }@



  • I found a solution. this code works great. You are able to detect a key pressed in a virtual keyboard. detecting the "return" key pressed in the textEdit will work.

    @ui->textEdit->installEventFilter(this);@

    put the above code in the constructor. change ui->textEdit to your widget.

    @bool MainWindow::eventFilter(QObject *object, QEvent *event)
    {
    if (object == ui->textEdit && event->type() == QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Return)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    }
    else
    {
    return QMainWindow::eventFilter(object, event);
    }
    }
    else
    {
    return QMainWindow::eventFilter(object, event);
    }
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.