imx6q红外按键控制QT界面光标移动

说明:本来想尝试做一个红外按键控制QT界面控件选中的,因为windows中pushbutton有三种状态,一种是常态,一种是光标停靠态还有一种是停靠态,想的是通过移动光标使pushbutton处于光标停靠状态来表示pushbutton被选中的,但是程序编译好在板子上跑,通过红外按键确实实现了光标移动,但是光标移动到相应控件上,控件并没有像windows上那样出现处于光标停靠态的效果。

        但想着实现了红外按键控制QT界面的光标移动也算是一种进步,就顺便记录下,已方便有同类似需求的人做一个参考。

**********************************************************************RFslect.pro******************************************

#-------------------------------------------------
#
# Project created by QtCreator 2018-05-15T18:50:25
#
#-------------------------------------------------

QT       += core gui


greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = RFslect
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp \

        widget.cpp


HEADERS += \

        widget.h

********************************************************widget.h********************************************************

#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include <QObject>
#include <QtWidgets>
#include <QString>
#include <QSocketNotifier>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <QDebug>
#include <linux/input.h>
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void initIO(void);
    void set_ui(void);
    void keyPressEvent(QKeyEvent * k);
private slots:
    void keyreadyread();
private:
    QSocketNotifier *notifier;   //红外读监听
   // QString * device;
    int  fd;    //文件描述符
    static int flag;     //按键按下记录标志
    QPoint * curentpiont;    //用于获取当前鼠标的坐标,也用于设置坐标改变
    QPushButton * p1;
    QPushButton * p2;
    QPushButton * p3;
    QPushButton * p4;
    QPushButton * p5;
    QPushButton * p6;
    QPushButton * p7;
    QHBoxLayout * layout1;
    QHBoxLayout * layout2;
};

#endif // WIDGET_H

***************************************************main.cpp***************************************************************

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();

}

*************************************************widget.cpp***************************************************************

#include "widget.h"
#include<QDebug>
int Widget::flag = 0;


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    curentpiont = new QPoint;
    *curentpiont = cursor().pos();   //获取当前光标所在位置
    qDebug()<<curentpiont->x()<<endl;
    qDebug()<<curentpiont->y()<<endl;
    initIO();      //红外按键相关初始化
    set_ui();     //程序界面设置
}
Widget::~Widget()
{
    if(this->fd>= 0)
    ::close(this->fd);
}

void Widget::initIO()
{
    this->fd= open("/dev/input/event0", O_RDONLY);     //打开红外按键设备
    if(this->fd< 0)
    {
        qDebug()<<"open file erro"<<endl;
        return;
    }
    this->notifier = new QSocketNotifier(this->fd, QSocketNotifier::Read, this); //监听读
    connect(this->notifier, SIGNAL(activated(int)), this, SLOT(keyreadyread()));  //如果读到的有数据则映射判断


}
void Widget::set_ui()
{
    p1 = new QPushButton("p1");
    p1->setFixedSize(50,100);
    p2 = new QPushButton("p2");
    p2->setFixedSize(50,100);
    p3 = new QPushButton("p3");
    p3->setFixedSize(50,100);
    p4 = new QPushButton("p4");
    p4->setFixedSize(50,100);
    p5 = new QPushButton("p5");
    p5->setFixedSize(50,100);
    layout1 = new QHBoxLayout;
    layout1->addWidget(p1);
    layout1->addSpacerItem(new QSpacerItem(30,30));
    layout1->addWidget(p2);
    layout1->addSpacerItem(new QSpacerItem(30,30));
    layout1->addWidget(p3);
    layout1->addSpacerItem(new QSpacerItem(30,30));
    layout1->addWidget(p4);
    layout1->addSpacerItem(new QSpacerItem(30,30));
    layout1->addWidget(p5);
    setLayout(layout1);
}
void Widget::keyPressEvent(QKeyEvent *k)
{
    if(k->key() == Qt::Key_Up)

    {

qDebug()<<"up is active"<<endl;

        curentpiont->setY(curentpiont->y()-30);    //坐标值改变
        QCursor::setPos(*curentpiont);        //重新设定光标位置
    }
    if(k->key() == Qt::Key_Down)
    {
  qDebug()<<"down is active"<<endl;
        curentpiont->setY(curentpiont->y()+30);
        QCursor::setPos(*curentpiont);
    }
    if(k->key() == Qt::Key_Left)
    {
  qDebug()<<"left is active"<<endl;
        curentpiont->setX(curentpiont->x()-30);
        QCursor::setPos(*curentpiont);
    }
    if(k->key() == Qt::Key_Right)
    {
  qDebug()<<"right is active"<<endl;
        curentpiont->setX(curentpiont->x()+30);
  qDebug()<<curentpiont->x()<<endl;
  qDebug()<<curentpiont->y()<<endl;
        QCursor::setPos(*curentpiont);
    }
    if(k->key() == Qt::Key_Enter)
    {
        qDebug()<<"ok is active"<<endl;
    }
}

//红外键值捕获
void Widget::keyreadyread()
{
    struct  input_event t;     //红外数据接收结构体
    if(read(fd,&t,sizeof(t))<0)
    {
        return;
    }
    Qt::KeyboardModifiers modifiers = Qt::NoModifier;
    int unicode = 0x0000; //unicode  用于保存映射之后的字符
    int keycode = 0; //keycode用于保存映射之后的Qt::Key类型的键值
    switch(t.code)
    {
        case 0x67: keycode = Qt::Key_Up; break;      
        case 0x6c: keycode = Qt::Key_Down; break;
        case 0x69: keycode = Qt::Key_Left; break;
        case 0x6a: keycode = Qt::Key_Right; break;
        case 0x160: keycode = Qt::Key_Enter; break;
        default:return;
    }
    if(1 == t.value )     //因为红外按键存在抖动问题,故每按下两次计数一次,来消除抖动
    {
        flag++;
    }
    if(2 == flag)
    {
        flag = 0;
        QKeyEvent keyPressEvent(QEvent::KeyPress, keycode, Qt::NoModifier);
        QGuiApplication::sendEvent(this, &keyPressEvent);   //发射按键事件
    }


}
















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兵叔物联

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值