PDA

View Full Version : Key Press Event Handling



kosasker
22nd March 2011, 12:34
I know, there is more thread about that in this forum. I looked all of them, but still i cant work it.

Vista Business
Qt SDK 2010.05
Qt Creator 2.0.1

I'm just trying to get which key is pressed.

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
private:
Ui::MainWindow *gui;

public:
explicit MainWindow(QWidget *parent = 0);

protected:
bool keypres(QKeyEvent *keyevent);

};

#endif // MAINWINDOW_H





mainwindow.cpp



#include "mainwindow.h"
#include <QKeyEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),gui(new Ui::MainWindow){
gui->setupUi(this);

gui->textEdit->installEventFilter(this);

}

bool MainWindow::keypres(QKeyEvent *keyevent)
{
if (keyevent->key()==Qt::Key_W)
{
qDebug() << "hi";
}
}


main.cpp



#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


I just want to control keys and key events.
Regards.

Gokulnathvc
22nd March 2011, 12:39
http://developer.qt.nokia.com/forums/viewthread/1243

http://stackoverflow.com/questions/3914809/solvedhow-to-propagate-keypressevent-on-different-qt-qmainwindow

I think this may help you

pan
22nd March 2011, 12:40
You need to set a focusPolicy

high_flyer
22nd March 2011, 12:51
Your event handler signature is wrong.
The documentation for QObject::installEventFilter() has an example exactly for key press events.
Why not start there?
http://doc.qt.nokia.com/latest/qobject.html#installEventFilter

kosasker
22nd March 2011, 14:54
Well then.
@pan : i added focus policy
and done somethings but thats have no effect.

i made it with QDialog base class. But with QMainWindows its not working. I'm tired.
I just open Qt Creator and create gui application, which is using QMainWindow base class.

mainwindows.cpp


#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::Dialog);
this->setFocusPolicy(Qt::NoFocus);
ui->textEdit->setFocusPolicy(Qt::StrongFocus);
ui->textEdit->installEventFilter(this);
}

bool MainWindow::catch_event(QObject *obj, QEvent *event)
{
if (obj == ui->textEdit && event->type()==QEvent::KeyPress)
{
QKeyEvent *keyevent = static_cast<QKeyEvent*>(event);
qDebug() << "key event";
return QObject::eventFilter(obj,event);
}
return QObject::eventFilter(obj,event);
}

Problem is not solved but i'm tired about 2 days. I will do with QDialog class.

Thanks all...

high_flyer
22nd March 2011, 15:15
Problem is not solved but i'm tired about 2 days.
I told you what the problem is!
And I gave a link to almost exactly the code you need, you only need to copy paste and introduce the changes in the handler to do the filtering based on the rules you need it.
So what is the problem?


heh... you really should read your own signature and apply some though on it...

kosasker
22nd March 2011, 15:27
Ok. Punch me :)
Bro its really not working. You think, i make joke ?
Try if you didnt believe ok.
Thanks so.

I'm missing something' s.. But i cant find it yet.

high_flyer
22nd March 2011, 15:33
:rolleyes:

The event filter filterObj receives events via its eventFilter() function.
not 'catch_event'.

all you had to do is COPY PASTE the example!!



//change the class name to your
bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
//and here put your own logic!!
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug("Ate key press %d", keyEvent->key());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}

kosasker
22nd March 2011, 15:49
Well, show me a corner. I will cry at there...



this->textEdit->installEventFilter(this);

I find where i missed up.
My goal is make a terminal program like putty.
Thanks. ;) :p