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. Capturing Enter Keydown Event
Forum Updated to NodeBB v4.3 + New Features

Capturing Enter Keydown Event

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.0k Views 3 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on last edited by
    #1

    Hello

    I am new to QT, but I have spent entirely too much time on this so I am reaching out for help.

    I really just want to capture when the user presses the enter key and acknowledge that with a QMessageBox.

    Here is the steps that I took

    1. Create a new 'QT Widgets Application' in 'QT Creator' and called the project 'SimpleCapture'

    2. Draw a Plain Text Edit on the main window

    3. Create a class called keyEnterReceiver

    This tells me to create a new class (not crazy about this because I need to update ui controls when the user presses enter)
    https://wiki.qt.io/How_to_catch_enter_key

    // keyEnterReceiver.h

    #ifndef KEYENTERRECEIVER_H
    #define KEYENTERRECEIVER_H
    
    #include <QKeyEvent>
    
    class keyEnterReceiver : public QObject
    {
    public:
        keyEnterReceiver();
        bool eventFilter(QObject* obj, QEvent* event);
    };
    
    #endif // KEYENTERRECEIVER_H
    

    // keyEnterReceiver.cpp

    #include "keyEnterReceiver.h"
    
    keyEnterReceiver::keyEnterReceiver()
    {
    
    }
    
    bool keyEnterReceiver::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) ) {
                //Enter or return was pressed
            } else {
                return QObject::eventFilter(obj, event);
            }
            return true;
        } else {
            return QObject::eventFilter(obj, event);
        }
        return false;
    }
    

    //mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "keyEnterReceiver.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        keyEnterReceiver* key = new keyEnterReceiver();
        ui->plainTextEdit->installEventFilter(key);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    My question is, how do I update gui items when the user presses enter? This seems overly complex for this functionality.

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What kind of update do you have in mind ?
      You could simply add a signal to your event filter and emit it on enter pressed. Then you can connect that signal to whatever you want to make the updates you require to your UI.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EverydayDiesel
        wrote on last edited by EverydayDiesel
        #3

        Thank you for the response.

        I need to

        1. Add text to another (main) text box
        2. Clear a few Plain Text Boxes
        3. Uncheck a few Check Boxes

        How would I go about modifying the example I made above to have the signal/event filter ?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          So the main window has some text inputs
          and you want to know if user press return
          in any of them, which then mean, "im done" ,
          much like the normal way where user clicks an ok button ?

          1 Reply Last reply
          0
          • E Offline
            E Offline
            EverydayDiesel
            wrote on last edited by
            #5

            yes, its a simple data entry program. Capturing that CHR(13) would really speed it up

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then as I suggested above, add a signal to your keyEnterReceiver class and emit it when the conditions fill what you want. Then connect that signal to whatever routine you want to that updates the GUI the way you want it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • E Offline
                E Offline
                EverydayDiesel
                wrote on last edited by
                #7

                Thank you for the reply.

                I feel I am getting close but still not there yet.

                //mainwindow.cpp

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                
                    keyEnterReceiver* key = new keyEnterReceiver();
                    ui->txtSongName->installEventFilter(key);
                
                    QObject::connect(key,
                                     SIGNAL(eventFilter()),
                                     this,
                                     SLOT(AddInformationAndResetForm()));
                }
                
                void MainWindow::AddInformationAndResetForm()
                {
                    QMessageBox::information(this, "worked", "it worked");
                }
                
                

                // keyEnterReceiver.cpp

                #include "keyEnterReceiver.h"
                
                keyEnterReceiver::keyEnterReceiver()
                {
                
                }
                
                bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
                {
                    qDebug() << "EVENT WAS CALLED";   // THIS WORKS!!!!!!!!!!!!!!!!!!!!!11
                    if (event->type()==QEvent::KeyPress)
                    {
                        QKeyEvent* key = static_cast<QKeyEvent*>(event);
                        if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) )
                        {
                            emit AddInformationAndResetForm();   // ERROR HAPPENS HERE###
                        }
                        else
                        {
                            return QObject::eventFilter(obj, event);
                        }
                        return true;
                    }
                    else
                    {
                        return QObject::eventFilter(obj, event);
                    }
                    return false;
                }
                

                Errors
                In member function ‘virtual bool keyEnterReceiver::eventFilter(QObject, QEvent)’:
                ‘AddInformationAndResetForm’ was not declared in this scope
                emit AddInformationAndResetForm();
                **

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  eventFilter is not a signal it's just a member function.

                  You should move to the new Qt 5 connect syntax. This will give you compile time errors in that kind of situation.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  E 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    eventFilter is not a signal it's just a member function.

                    You should move to the new Qt 5 connect syntax. This will give you compile time errors in that kind of situation.

                    E Offline
                    E Offline
                    EverydayDiesel
                    wrote on last edited by EverydayDiesel
                    #9

                    I have no idea why I am having such a hard time with this.

                    @sgaist said in Capturing Enter Keydown Event:

                    eventFilter is not a signal it's just a member function.

                    Do you mean this?
                    // keyEnterReceiver.h

                    #ifndef KEYENTERRECEIVER_H
                    #define KEYENTERRECEIVER_H
                    
                    #include <QDebug>
                    #include <QObject>
                    #include <QKeyEvent>
                    
                    class keyEnterReceiver : public QObject
                    {
                        Q_OBJECT
                    public:
                        keyEnterReceiver();
                    
                    
                    signals:
                            bool eventFilter(QObject* obj, QEvent* event);
                    
                    public slots:
                    
                    
                    };
                    
                    #endif // KEYENTERRECEIVER_H
                    

                    @sgaist said in Capturing Enter Keydown Event:

                    You should move to the new Qt 5 connect syntax. This will give you compile time errors in that kind of situation.

                    I set this up in main but the problem is keyEnterReciever::eventFilter() takes two paramaters which I do not have when the forms constructor

                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        keyEnterReceiver* key = new keyEnterReceiver();
                        ui->txtSongName->installEventFilter(key);
                    
                    
                    
                        connect(
                            key,
                            &keyEnterReceiver::eventFilter(),
                            AddInformationAndResetForm()
                        );
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by SGaist
                      #10

                      Again: eventFilter is not a signal.

                      It's an existing method, you can't make it a signal.

                      Did you read the Signals And Slots chapter in Qt's documentation ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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