Capturing Enter Keydown Event
-
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
-
Create a new 'QT Widgets Application' in 'QT Creator' and called the project 'SimpleCapture'
-
Draw a Plain Text Edit on the main window
-
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; }
#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
-
-
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. -
Thank you for the response.
I need to
- Add text to another (main) text box
- Clear a few Plain Text Boxes
- Uncheck a few Check Boxes
How would I go about modifying the example I made above to have the signal/event filter ?
-
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 ? -
yes, its a simple data entry program. Capturing that CHR(13) would really speed it up
-
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.
-
Thank you for the reply.
I feel I am getting close but still not there yet.
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();** -
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.
-
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.
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() );
-
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 ?