[SOLVED] Help with keypress event
-
Hi all!
I am trying to have a keypress event in my mainwindow. But on running the application, no event occurs when I press the specific key. Can you please have a look at my code down below and guide me if I'm wrong?
Thanks.mainwindow.h
@
#include <QKeyEvent>namespace Ui {
class MainWindow;
}class MainWindow: public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();protected:
void keypress(QKeyEvent *e);};
#endif
@my mainwindow.cpp
@
#include "MainWindow.h"
#include "ui_MainWindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}void MainWindow::keypress(QKeyEvent *keyevent)
{
if (keyevent->key() == Qt::Key_Escape)
this->close();
}ProgramBanner::~ProgramBanner()
{
delete ui;
}
@ -
Pls check this "thread":http://qt-project.org/forums/viewthread/1243
-
[quote author="Sam" date="1358924236"]You just need to change
line 17: of mainwindow.h to
@void keyPressEvent(QKeyEvent *event);@
and same for your .cpp file
check "keyPressEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#keyPressEvent[/quote]
I think it doesn't matter what you use. *keyevent or *event is still the same.
EDIT: sorry Sam, i rechecked and found out that it wasn't the right function that was used.
-
I tried using *event in both my .cpp and .h file. But nothing is happening. Is it something to do with setFocusPolicy?
I tried reading "this":http://www.qtcentre.org/threads/45159-keyPressEvent-not-being-called but I still couldn't get my program to work.
Please help! -
I tried a sample code the same that you have :
@#include <QMainWindow>
#include <QKeyEvent>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;protected:
void keyPressEvent(QKeyEvent *event);
};@.cpp
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::keyPressEvent(QKeyEvent *event)
{if (event->key() == Qt::Key_Escape) this->close();
}@
works as expected as there is nothing inside the mainwindow, other option can be to use eventFilter and there also you can check for keypressevent.
something like
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
installEventFilter(this);
}MainWindow::~MainWindow()
{
delete ui;
}bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent keyEvent = static_cast<QKeyEvent>(event);if (keyEvent->key() == Qt::Key_Escape) { this->close(); return QMainWindow::eventFilter(target,event); } } return QMainWindow::eventFilter(target,event);
}@
-
Finally! It worked. I tried using installEventFilter earlier but I was clear on how to use it so I went back to the regular keypress event which didn't work.
Thank you so much for your time and help.
If it's not too much trouble, could you please explain why eventfilter worked but not my earlier code? I'm new to Qt and the documentation doesn't always help me understand things.
Thanks again!
-
[quote author="holygirl" date="1358929707"]I tried using *event in both my .cpp and .h file. But nothing is happening. Is it something to do with setFocusPolicy?
I tried reading "this":http://www.qtcentre.org/threads/45159-keyPressEvent-not-being-called but I still couldn't get my program to work.
Please help![/quote]I was not talking about changing *e to *event , Its about changing keypress to keyPressEvent.