Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] textEdit return key was pressed
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] textEdit return key was pressed

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 12.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on 15 Aug 2011, 23:57 last edited by
    #1

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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on 16 Aug 2011, 00:54 last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on 16 Aug 2011, 04:54 last edited by
        #3

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

        }@

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on 16 Aug 2011, 08:03 last edited by
          #4

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

          M 1 Reply Last reply 3 Dec 2023, 19:14
          0
          • K kalster
            16 Aug 2011, 08:03

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

            M Offline
            M Offline
            MMD18
            wrote on 3 Dec 2023, 19:14 last edited by
            #5

            @kalster great solution. Even after 11 years it is still usefull.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved