- 积分
- 52
贡献35
飞刀0 FD
注册时间2012-3-17
在线时间31 小时

扫一扫,手机访问本帖 
|
我用Qt Creator编的(QT4.7.1下)的Qt窗口程序(基本上就是飞凌提供的源码)测试OK210开发板自带的几个按键时,出现这样的情况:按键后程序执行效果与按USB键盘的上下左右箭头键效果相同,而不是程序预想的效果(显示出按键名称),感觉按开发板的键时没有产生信号(SIGNAL(activated(int)))或者是信号被QT窗口等给截取了,不能转到相应的槽函数中执行。另外,在自编的QT界面程序中,采用飞凌相应的命令行测试源码方式来测试,跟追结果是一直停在read语句上,按键也没反应。但直接执行飞凌自带的程序(QT界面和命令行),结果都是正确的。
请高人指点一下,问题出在哪里?非常感谢!下面是我的代码:
//.h文件
#ifndef DIALOG_OK210_H
#define DIALOG_OK210_H
#include <qsocketnotifier.h>
//--------测试按键要用到的数据结构
#define KEYDEVICE "/dev/input/event0"
class QSocketNotifier; //need this class
struct InputData
{
unsigned int dummy1;
unsigned int dummy2;
unsigned short type;
unsigned short code;
unsigned int value;
unsigned int dummy3;
unsigned int dummy4;
unsigned int dummy5;
unsigned int dummy6;
};
#define KEY_UP 103 // key 1
#define KEY_DOWN 108 // key 2
#define KEY_LEFT 105 // key 3
#define KEY_RIGHT 106 // key 4
#define KEY_ENTER 28 // key 5
#define KEY_ESC 1 // key 6
//---------------------------------------
#include <QDialog>
namespace Ui {
class Dialog_OK210;
}
class Dialog_OK210 : public QDialog
{
Q_OBJECT
public:
explicit Dialog_OK210(QWidget *parent = 0);
~Dialog_OK210();
private:
Ui::Dialog_OK210 *ui;
//按键测试
private:
int m_fd_key;
QSocketNotifier* m_notifyObject;
private slots:
void key_Event();
};
#endif // DIALOG_OK210_H
//.cpp文件
#include "dialog_OK210.h"
#include "ui_dialog_OK210.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
Dialog_OK210::Dialog_OK210(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_OK210)
{
ui->setupUi(this);
m_fd_key = ::open(KEYDEVICE, O_RDONLY | O_NONBLOCK);
if (m_fd_key < 0)
{
return;
}
m_notifyObject = new QSocketNotifier(m_fd_key, QSocketNotifier::Read, this);
connect (m_notifyObject, SIGNAL(activated(int)), this, SLOT(key_Event()));
}
Dialog_OK210::~Dialog_OK210()
{
delete m_notifyObject;
if (m_fd_key >=0)
::close(m_fd_key);
delete ui;
}
//按键测试槽函数
void Dialog_OK210::key_Event()
{
struct InputData event;
int nResult=::read(m_fd_key, &event, sizeof(event));
if (nResult != sizeof(InputData))
{
return;
}
//在一个文本标签上显示按键名称
switch(event.code)
{
case KEY_UP:
ui->label_key->setText("UP");
break;
case KEY_DOWN:
ui->label_key->setText("DOWN");
break;
case KEY_LEFT:
ui->label_key->setText("LEFT");
break;
case KEY_RIGHT:
ui->label_key->setText("RIGHT");
break;
case KEY_ENTER:
ui->label_key->setText("ENTER");
break;
case KEY_ESC:
ui->label_key->setText("ESC");
break;
default:
{
ui->label_key->setText("其它");
break;
}
}
}
|
|