最近几天在网上搜索一下关于QtE自定义按键的文章,看了有些收获,拿出来大家讨论讨论
 
公司是通过串口接的键盘,通过串口接键盘首先要有串口驱动,设置串口(波动率,数据位,停止位,数据怎样校验)
 
--------------------------test.h------------------------------------------------------
 
#ifndef TEST_H
#define TEST_H
class test :public QWidget
{
Q_OBJECT
public :
          test();
          int fd;
            QSocketNotifier       *notifier;
public slots:
        void readKeyboardData();
};
#endif
 
 
 
------------------------------test.cpp------------------------------
#include     <stdio.h>      /*标准输入输出定义*/
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix 标准函数定义*/
#include     <sys/types.h>  
#include     <sys/stat.h>   
#include     <fcntl.h>      /*文件控制定义*/
#include     <termios.h>    /*PPSIX 终端控制定义*/
#include     <errno.h>      /*错误号定义*/
 
#include "test.h"struct termios opt;
test::test()
{
    fd=-1;
    fd=open("/dev/ttyS0",O_RDONLY | O_NDELAY);
   // 设置串口属性
      cfsetispeed(&opt, B9600);
      cfsetospeed(&opt, B9600);
           opt.c_cflag |=CLOCAL | CREAD;
          opt.c_cflag &= ~CSIZE;
          opt.c_cflag |=CS8;
          opt.c_cflag &= ~PARENB;
           opt.c_cflag &= ~CSTOPB; 
           tcsetattr(fd,TCSANOW,&opt);
    if(fd>0)
    {
                notifier = new QSocketNotifier( fd, QSocketNotifier::Read, this );
                connect( notifier, SIGNAL(activated(int)),this,SLOT(readKeyboardData()) );
    }
 
}
 
void test::readKeyboardData()
{
       char buf;
       int codekey;
       read(fd,buf,sizeof(buf));
       switch(buf)
      {
       case '1':
        codekey=Qt::Key_Left;
        break;
        case '2':
       codekey=Qt::Key_Right;
        break;
        case '3':
        codekey=Qt::Key_Up;
        break;
        case '4':
       codekey=Qt::Key_Down;
       break;
      }
    QWSServer::processKeyEvent(0,codekey,Qt::NoModifier,true,false);
}
 
int  main(int argc,char **argv)
{
    QApplication app(argc,argv);
    test ts;
    ts.show();
    return app.exec()
}