gunturrohith
5th December 2015, 06:51
Hi,
I need to write an application that helps me to interchange the behaviour of the keys,
i.e I need to swap the functionality of the both up arrow and down arrow keys that are present in the keyboard and i am trying to do it using the QKeyEvent.
Is it possible to do so,here is my code please go through it and suggest me where i have done wrong
Here is My HeaderFile
#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
#include <QEvent>
#include<QTextEdit>
#include<QMessageBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
// QMessageBox box;
// QLabel *txt;
~Widget();
protected:
bool event(QEvent *);
};
#endif // WIDGET_H
Here is My ".cpp" File
#include "widget.h"
#include <QDebug>
#include <QKeyEvent>
#include<QTextEdit>
#include<QString>
#include<QMessageBox>
QString str,str1,str2;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qDebug() << "Constructor";
//txt= new QLabel(this);
//txt->setGeometry(0,0,320,240);
}
Widget::~Widget()
{
qDebug() << "Destructor";
}
bool Widget::event(QEvent *e){
//int ret;
// qDebug()<<"line num is"<<__LINE__;
if(e->type()==QEvent::KeyRelease){
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
int keyValR = ke->key();
qDebug() << "Released key value is : " << keyValR;
/* QString str = QString::number(keyValR);
qDebug()<<str;*/
switch (keyValR) {
case Qt::Key_F1 :
//qDebug()<<__LINE__;
qDebug() << "You have PRESSED :: F1";
//qDebug() << "You have PRESSED :: F1";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F1</font></b>");
break;
//case 16777265:
case Qt::Key_F2 :
qDebug() << "You have PRESSED :: F2";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F2</font></b>");
break;
case Qt::Key_F3 :
qDebug() << "You have PRESSED :: F3";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F3</font></b>");
break;
case Qt::Key_F4 :
qDebug() << "You have PRESSED :: F4";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F4</font></b>");
break;
case Qt::Key_F5 :
qDebug() << "You have PRESSED :: F5";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F5</font></b>");
case Qt::Key_F6 :
qDebug() << "You have PRESSED :: F6";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F6</font></b>");
break;
case Qt::Key_F7 :
qDebug() << "You have PRESSED :: F7";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F7</font></b>");
break;
case Qt::Key_F8 :
qDebug() << "You have PRESSED :: F8";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F8</font></b>");
break;
case Qt::Key_F9 :
qDebug() << "You have PRESSED :: F9";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F9</font></b>");
break;
case Qt::Key_F10 :
qDebug() << "You have PRESSED :: F10";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F10</font></b>");
break;
case Qt::Key_F11 :
qDebug() << "You have PRESSED :: F11";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F11</font></b>");
break;
case Qt::Key_F12 :
qDebug() << "You have PRESSED :: F12";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F12</font></b>");
break;
case Qt::Key_Return :
qDebug() << "You have PRESSED :: RETURN";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>RETURN</font></b>");
break;
case Qt::Key_Enter :
qDebug() << "You have PRESSED :: ENTER";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>ENTER</font></b>");
break;
case Qt::Key_Space :
qDebug() << "You have PRESSED :: SPACE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SPACE</font></b>");
break;
case Qt::Key_Shift :
qDebug() << "You have PRESSED :: SHIFT";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SHIFT</font></b>");
break;
case Qt::Key_CapsLock :
qDebug() << "You have PRESSED :: CAPSLOCK";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>CAPSLOCK</font></b>");
break;
case Qt::Key_Delete :
qDebug() << "You have PRESSED :: DELETE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DELETE</font></b>");
break;
case Qt::Key_Left:
qDebug() << "You have PRESSED :: <-";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red> <- </font></b>");
break;
case Qt::Key_Right:
qDebug() << "You have PRESSED ::->";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>-></font></b>");
break;
case Qt::Key_Down:
qDebug() << "You have PRESSED :: uparrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>UPARROW</font></b>");
break;
case Qt::Key_Up :
qDebug() << "You have PRESSED :: DownArrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DownArrow</font></b>");
default:
str = ke->text();
qDebug() << "You have PRESSED :: " << str;
str = str.prepend("You have PRESSED :: <b><font color = red>");
str = str.append("</font></b>");
QMessageBox::information(this,"key pressed", str);
break;
}
}
Please help me ASAP
Thanks in advance
Regards,
Rohith.G
I need to write an application that helps me to interchange the behaviour of the keys,
i.e I need to swap the functionality of the both up arrow and down arrow keys that are present in the keyboard and i am trying to do it using the QKeyEvent.
Is it possible to do so,here is my code please go through it and suggest me where i have done wrong
Here is My HeaderFile
#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
#include <QEvent>
#include<QTextEdit>
#include<QMessageBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
// QMessageBox box;
// QLabel *txt;
~Widget();
protected:
bool event(QEvent *);
};
#endif // WIDGET_H
Here is My ".cpp" File
#include "widget.h"
#include <QDebug>
#include <QKeyEvent>
#include<QTextEdit>
#include<QString>
#include<QMessageBox>
QString str,str1,str2;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qDebug() << "Constructor";
//txt= new QLabel(this);
//txt->setGeometry(0,0,320,240);
}
Widget::~Widget()
{
qDebug() << "Destructor";
}
bool Widget::event(QEvent *e){
//int ret;
// qDebug()<<"line num is"<<__LINE__;
if(e->type()==QEvent::KeyRelease){
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
int keyValR = ke->key();
qDebug() << "Released key value is : " << keyValR;
/* QString str = QString::number(keyValR);
qDebug()<<str;*/
switch (keyValR) {
case Qt::Key_F1 :
//qDebug()<<__LINE__;
qDebug() << "You have PRESSED :: F1";
//qDebug() << "You have PRESSED :: F1";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F1</font></b>");
break;
//case 16777265:
case Qt::Key_F2 :
qDebug() << "You have PRESSED :: F2";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F2</font></b>");
break;
case Qt::Key_F3 :
qDebug() << "You have PRESSED :: F3";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F3</font></b>");
break;
case Qt::Key_F4 :
qDebug() << "You have PRESSED :: F4";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F4</font></b>");
break;
case Qt::Key_F5 :
qDebug() << "You have PRESSED :: F5";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F5</font></b>");
case Qt::Key_F6 :
qDebug() << "You have PRESSED :: F6";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F6</font></b>");
break;
case Qt::Key_F7 :
qDebug() << "You have PRESSED :: F7";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F7</font></b>");
break;
case Qt::Key_F8 :
qDebug() << "You have PRESSED :: F8";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F8</font></b>");
break;
case Qt::Key_F9 :
qDebug() << "You have PRESSED :: F9";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F9</font></b>");
break;
case Qt::Key_F10 :
qDebug() << "You have PRESSED :: F10";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F10</font></b>");
break;
case Qt::Key_F11 :
qDebug() << "You have PRESSED :: F11";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F11</font></b>");
break;
case Qt::Key_F12 :
qDebug() << "You have PRESSED :: F12";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>F12</font></b>");
break;
case Qt::Key_Return :
qDebug() << "You have PRESSED :: RETURN";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>RETURN</font></b>");
break;
case Qt::Key_Enter :
qDebug() << "You have PRESSED :: ENTER";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>ENTER</font></b>");
break;
case Qt::Key_Space :
qDebug() << "You have PRESSED :: SPACE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SPACE</font></b>");
break;
case Qt::Key_Shift :
qDebug() << "You have PRESSED :: SHIFT";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>SHIFT</font></b>");
break;
case Qt::Key_CapsLock :
qDebug() << "You have PRESSED :: CAPSLOCK";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>CAPSLOCK</font></b>");
break;
case Qt::Key_Delete :
qDebug() << "You have PRESSED :: DELETE";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DELETE</font></b>");
break;
case Qt::Key_Left:
qDebug() << "You have PRESSED :: <-";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red> <- </font></b>");
break;
case Qt::Key_Right:
qDebug() << "You have PRESSED ::->";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>-></font></b>");
break;
case Qt::Key_Down:
qDebug() << "You have PRESSED :: uparrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>UPARROW</font></b>");
break;
case Qt::Key_Up :
qDebug() << "You have PRESSED :: DownArrow";
QMessageBox::information(this,"key pressed", "You have PRESSED :: <b><font color = red>DownArrow</font></b>");
default:
str = ke->text();
qDebug() << "You have PRESSED :: " << str;
str = str.prepend("You have PRESSED :: <b><font color = red>");
str = str.append("</font></b>");
QMessageBox::information(this,"key pressed", str);
break;
}
}
Please help me ASAP
Thanks in advance
Regards,
Rohith.G