I am trying to learn how to make software with GUIs using QtCreator. I've done some programming before, but never anything this involved. So far, I have made a window with 5 items: 2 labels, 1 button, 1 lineEdit, and 1 listWidget.
My goal is to be able to enter text in the lineEdit and make it appear in the listWidget. If you click on the button with the mouse, this works fine.
I would also like to be able to use the enter/return key on a keyboard to activate the button. This is the part I need help with.
I created a new class for handling key events called KeyboardFilter.
I have installed the event filter on the main window object. The eventFilter function should receive any event, check if it's a key event, then check if it's the enter button. If it is, then I want to activate the button.
I can't tell if any of the stuff I've written for the eventFilter is actually working.
// == keyboardfilter.h =======================================================
#ifndef KEYBOARDFILTER_H
#define KEYBOARDFILTER_H
#include <QApplication>
#include <QLineEdit>
#include <QKeyEvent>
class KeyboardFilter : public QObject
{
public:
KeyboardFilter( QObject *parent = nullptr ) : QObject( parent ) {}
//protected:
public:
bool eventFilter( QObject *target, QEvent *event );
};
#endif // KEYBOARDFILTER_H
// == mainwindow.h ===========================================================
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
// handles clicking the enter button with the mouse
//private slots:
public slots:
void EnterPressed();
// supposed to handle key presses. doesn't actually work
//protected:
public:
void KeyPressEvent(QKeyEvent *event);
void KeyReleaseEvent(QKeyEvent *event);
bool EventFilter(QEvent *event);
};
#endif // MAINWINDOW_H
// == keyboardfilter.cpp =====================================================
#include "keyboardfilter.h"
bool KeyboardFilter::eventFilter(QObject *target, QEvent *event)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
return true;
}
}
if(event->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
return true;
}
}
return false;
}
// == mainwindow.cpp =========================================================
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include <QKeyEvent>
#include <iostream>
QString stack[10];
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
stack[1] = "stack";
ui->Display->addItem(stack[1]);
connect(ui->Enter, SIGNAL(released()), this, SLOT(EnterPressed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//qDebug() << "Debug Message";
//QDebug("text");
void MainWindow::EnterPressed(){
//ui->Input->setText(QString("text"));
ui->Display->clear();
QString input = ui->Input->text();
ui->Input->clear();
ui->Display->addItem(input);
}
// -- keyboardfilter.cpp
#include "keyboardfilter.h"
bool KeyboardFilter::eventFilter(QObject *target, QEvent *event)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
return true;
}
}
if(event->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter){
return true;
}
}
return false;
}
// == main.cpp ===============================================================
#include "mainwindow.h"
#include "keyboardfilter.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// KeyboardFilter filter;
// a.installEventFilter(&filter);
KeyboardFilter* key = new KeyboardFilter();
w.installEventFilter(key);
if(key){
w.EnterPressed();
}
return a.exec();
}
When I run this code, the window pops up and I can enter text into the lineEdit. If I click the button with the mouse, the text gets moved to the listWidget, as desired. If I enter text and then hit "Enter", nothing happens.
I have tried hitting tab to give focus to the lineEdit, listWidget, and button before hitting enter, but hasn't helped though.