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. Can't catch Enter (Return)
Qt 6.11 is out! See what's new in the release blog

Can't catch Enter (Return)

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.9k Views
  • 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.
  • C Offline
    C Offline
    Chaki
    wrote on last edited by
    #1

    Hello,
    I have tried the following code to qDebug something when I click the Enter Button, but nothing happens:

    #include <QKeyEvent>
    bool MainWindow::eventFilter(QObject* obj, QEvent* event)
    {
         if (event->type()==QEvent::KeyPress) {
            QKeyEvent* key = static_cast<QKeyEvent*>(event);
            if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                qDebug()<< "pressed";
            } else {
                return QObject::eventFilter(obj, event);
            }
            return true;
        } else {
            return QObject::eventFilter(obj, event);
        }
        return false;
    
    }
    private slots:
        bool eventFilter(QObject* obj, QEvent* event);
    JonBJ jsulmJ 2 Replies Last reply
    0
    • C Chaki

      Hello,
      I have tried the following code to qDebug something when I click the Enter Button, but nothing happens:

      #include <QKeyEvent>
      bool MainWindow::eventFilter(QObject* obj, QEvent* event)
      {
           if (event->type()==QEvent::KeyPress) {
              QKeyEvent* key = static_cast<QKeyEvent*>(event);
              if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                  qDebug()<< "pressed";
              } else {
                  return QObject::eventFilter(obj, event);
              }
              return true;
          } else {
              return QObject::eventFilter(obj, event);
          }
          return false;
      
      }
      private slots:
          bool eventFilter(QObject* obj, QEvent* event);
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Chaki
      Where is the focus when you click the Enter key? Is it on a widget, or do you have a blank main window?

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Chaki
        wrote on last edited by
        #3

        @JonB I am sorry, but I did not quite understand what you mean.
        Coul you explain it please?

        1 Reply Last reply
        0
        • C Chaki

          Hello,
          I have tried the following code to qDebug something when I click the Enter Button, but nothing happens:

          #include <QKeyEvent>
          bool MainWindow::eventFilter(QObject* obj, QEvent* event)
          {
               if (event->type()==QEvent::KeyPress) {
                  QKeyEvent* key = static_cast<QKeyEvent*>(event);
                  if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                      qDebug()<< "pressed";
                  } else {
                      return QObject::eventFilter(obj, event);
                  }
                  return true;
              } else {
                  return QObject::eventFilter(obj, event);
              }
              return false;
          
          }
          private slots:
              bool eventFilter(QObject* obj, QEvent* event);
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Chaki said in Can't catch Enter (Return):

          when I click the Enter Button

          Do you mean: when I press the enter key?
          Or are you using virtual keyboard?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chaki
            wrote on last edited by
            #5

            @jsulm i mean the enter key

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chaki
              wrote on last edited by
              #6

              I have solved it:

                  connect(ui->line_Edit, SIGNAL(returnPressed()),ui->btn_Button,SIGNAL(clicked()));
              JonBJ 1 Reply Last reply
              0
              • C Chaki

                I have solved it:

                    connect(ui->line_Edit, SIGNAL(returnPressed()),ui->btn_Button,SIGNAL(clicked()));
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Chaki
                Which is what I suggested: you are pressing the Enter key on a QLineEdit, but you did not tell us that, it helps if you do! :) That line edit has the focus, so the key press will not get sent to the main window....

                1 Reply Last reply
                3
                • C Offline
                  C Offline
                  Chaki
                  wrote on last edited by
                  #8

                  @JonB Yes, you're right sir. Thanks you your reply

                  1 Reply Last reply
                  2

                  • Login

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