您的位置:首页 > 编程语言 > Qt开发

qt5 keyboardless & mouseless

2015-05-18 15:50 225 查看
实现方法:

单独起一个线程,用于读取按键代码,读取后转换为Qt::Keys,然后使用

QApplication::postEvent((QObject*)tFocusObj,tTabKeyPress);

进行分发到当前具有焦点的窗体上。

#include "zkeypadthread.h"

ZKeyPadThread::ZKeyPadThread(QObject *parent) :

QObject(parent)

{

this->m_udpSocket=new QUdpSocket;

connect(this->m_udpSocket,SIGNAL(readyRead()),this,SLOT(ZSlotReadUDPPacket()));

}

ZKeyPadThread::~ZKeyPadThread()

{

delete this->m_udpSocket;

}

qint32 ZKeyPadThread::ZStartListen()

{

if(this->m_udpSocket->bind(QHostAddress::Any,1986))

{

return 0;

}else{

return -1;

}

}

qint32 ZKeyPadThread::ZStopListen()

{

this->m_udpSocket->disconnectFromHost();

this->m_udpSocket->close();

return 0;

}

void ZKeyPadThread::ZSlotReadUDPPacket()

{

QByteArray tUDPSyncHeader("EAVKeyPad");

QByteArray tRecvBuffer;

tRecvBuffer.resize(this->m_udpSocket->pendingDatagramSize());

this->m_udpSocket->readDatagram(tRecvBuffer.data(),tRecvBuffer.size());

if(!tRecvBuffer.startsWith(tUDPSyncHeader))

{

qDebug()<<"unknow udp sync header!";

return;

}

qDebug()<<tRecvBuffer;

tRecvBuffer.remove(0,tUDPSyncHeader.size());

qDebug()<<tRecvBuffer;

for(qint32 i=0;i<tRecvBuffer.size();i++)

{

qDebug()<<tRecvBuffer.at(i);

qint32 tKeyCode=0;

switch(tRecvBuffer.at(i))

{

case ZKeyPadThread::ZKeyPad_Top:

tKeyCode=Qt::Key_Up;

break;

case ZKeyPadThread::ZKeyPad_Bottom:

tKeyCode=Qt::Key_Down;

break;

case ZKeyPadThread::ZKeyPad_Left:

tKeyCode=Qt::Key_Left;

break;

case ZKeyPadThread::ZKeyPad_Right:

tKeyCode=Qt::Key_Right;

break;

case ZKeyPadThread::ZKeyPad_Enter:

tKeyCode=Qt::Key_Enter;

break;

case ZKeyPadThread::ZKeyPad_Return:

tKeyCode=Qt::Key_Escape;

break;

default:

break;

}

//post key event.

QWidget *tFocusObj=QApplication::activeWindow();

if(tFocusObj)

{

QKeyEvent *tTabKeyPress=new QKeyEvent( QEvent::KeyPress,tKeyCode,Qt::NoModifier);

QApplication::postEvent((QObject*)tFocusObj,tTabKeyPress);

}

}

}

然后在主窗体上使用二组数据组织各个部件,(x,y),

根据上下左右控制x和y的+1或-1,然后赋予某个部件焦点。

#ifndef ZHOMENAVIGATOR_H

#define ZHOMENAVIGATOR_H

#include <QFrame>

#include <QGridLayout>

#include <BaseParts/zcoolbutton.h>

#include <MainUI/zfilemanager.h>

#include <MainUI/zsystemsetup.h>

#include <MainUI/zdatetimesetup.h>

#include <Parameters/zparameters.h>

#include <QHash>

#include <QList>

#include <QPoint>

typedef struct

{

QPoint pos;

QWidget *element;

}ZKeyElement;

class ZHomeNavigator : public QFrame

{

Q_OBJECT

public:

explicit ZHomeNavigator(QWidget *parent = 0);

~ZHomeNavigator();

protected:

void keyPressEvent(QKeyEvent *event);

void keyReleaseEvent(QKeyEvent *event);

signals:

void ZSignalReturnButtonClicked();

public slots:

void ZSlotShowFileManager();

void ZSlotShowSystemSetup();

void ZSlotShowDateTimeSetup();

private:

void ZAddWidgetToKeyList(qint32 x,qint32 y,QWidget *widget);

private:

QToolButton *m_btnFileManager;

QToolButton *m_btnSysetmSetup;

QToolButton *m_btnWireless;

QToolButton *m_btnDateTime;

QToolButton *m_btnDevLog;

QToolButton *m_btnBattery;

QToolButton *m_btnSound;

QToolButton *m_btnShutdown;

QToolButton *m_btnReturn;

QGridLayout *m_gridLayout;

//keys move hash.

QList<ZKeyElement> m_keysList;

QPoint m_currentKey;

qint32 m_KeyRowCount;

qint32 m_KeyColCount;

};

#endif // ZHOMENAVIGATOR_H

#include "zhomenavigator.h"

ZHomeNavigator::ZHomeNavigator(QWidget *parent) :

QFrame(parent)

{

this->setObjectName("ZHomeNavigator");

this->setFocusPolicy(Qt::StrongFocus);

this->m_btnFileManager=new QToolButton;

this->m_btnFileManager->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnFileManager->setIconSize(QSize(48,48));

this->m_btnFileManager->setIcon(QIcon(":/images/znavigator_filemanager.png"));

this->m_btnFileManager->setText(tr("Videos"));

connect(this->m_btnFileManager,SIGNAL(clicked()),this,SLOT(ZSlotShowFileManager()));

this->m_btnSysetmSetup=new QToolButton;

this->m_btnSysetmSetup->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnSysetmSetup->setIconSize(QSize(48,48));

this->m_btnSysetmSetup->setIcon(QIcon(":/images/znavigator_setup.png"));

this->m_btnSysetmSetup->setText(tr("Settings"));

connect(this->m_btnSysetmSetup,SIGNAL(clicked()),this,SLOT(ZSlotShowSystemSetup()));

this->m_btnDateTime=new QToolButton;

this->m_btnDateTime->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnDateTime->setIconSize(QSize(48,48));

this->m_btnDateTime->setIcon(QIcon(":/images/znavigator_datetime.png"));

this->m_btnDateTime->setText(tr("DateTime"));

connect(this->m_btnDateTime,SIGNAL(clicked()),this,SLOT(ZSlotShowDateTimeSetup()));

this->m_btnWireless=new QToolButton;

this->m_btnWireless->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnWireless->setIconSize(QSize(48,48));

this->m_btnWireless->setIcon(QIcon(":/images/znavigator_wireless.png"));

this->m_btnWireless->setText(tr("Wireless"));

this->m_btnDevLog=new QToolButton;

this->m_btnDevLog->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnDevLog->setIconSize(QSize(48,48));

this->m_btnDevLog->setIcon(QIcon(":/images/znavigator_devlog.png"));

this->m_btnDevLog->setText(tr("DevLog"));

this->m_btnBattery=new QToolButton;

this->m_btnBattery->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnBattery->setIconSize(QSize(48,48));

this->m_btnBattery->setIcon(QIcon(":/images/znavigator_battery.png"));

this->m_btnBattery->setText(tr("Battery"));

this->m_btnSound=new QToolButton;

this->m_btnSound->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnSound->setIconSize(QSize(48,48));

this->m_btnSound->setIcon(QIcon(":/images/znavigator_sound.png"));

this->m_btnSound->setText(tr("Sound"));

this->m_btnShutdown=new QToolButton;

this->m_btnShutdown->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnShutdown->setIconSize(QSize(48,48));

this->m_btnShutdown->setIcon(QIcon(":/images/znavigator_shutdown.png"));

this->m_btnShutdown->setText(tr("Shutdown"));

this->m_btnReturn=new QToolButton;

this->m_btnReturn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

this->m_btnReturn->setIconSize(QSize(48,48));

this->m_btnReturn->setIcon(QIcon(":/images/znavigator_return.png"));

this->m_btnReturn->setText(tr("Return"));

connect(this->m_btnReturn,SIGNAL(clicked()),this,SIGNAL(ZSignalReturnButtonClicked()));

//layout.

this->m_gridLayout=new QGridLayout;

this->m_gridLayout->addWidget(this->m_btnFileManager,0,0,1,1);

this->m_gridLayout->addWidget(this->m_btnSysetmSetup,0,1,1,1);

this->m_gridLayout->addWidget(this->m_btnDateTime,0,2,1,1);

this->m_gridLayout->addWidget(this->m_btnWireless,0,3,1,1);

this->m_gridLayout->addWidget(this->m_btnDevLog,0,4,1,1);

this->m_gridLayout->addWidget(this->m_btnBattery,1,0,1,1);

this->m_gridLayout->addWidget(this->m_btnSound,1,1,1,1);

this->m_gridLayout->addWidget(this->m_btnShutdown,1,3,1,1);

this->m_gridLayout->addWidget(this->m_btnReturn,1,4,1,1);

//layout.

this->setLayout(this->m_gridLayout);

//arrange the widgets to control by keys.

this->ZAddWidgetToKeyList(0,0,this->m_btnFileManager);

this->ZAddWidgetToKeyList(0,1,this->m_btnSysetmSetup);

this->ZAddWidgetToKeyList(0,2,this->m_btnDateTime);

this->ZAddWidgetToKeyList(0,3,this->m_btnWireless);

this->ZAddWidgetToKeyList(0,4,this->m_btnDevLog);

this->ZAddWidgetToKeyList(1,0,this->m_btnBattery);

this->ZAddWidgetToKeyList(1,1,this->m_btnSound);

this->ZAddWidgetToKeyList(1,3,this->m_btnShutdown);

this->ZAddWidgetToKeyList(1,4,this->m_btnReturn);

//the initial key element is (0,0) at default.

this->m_currentKey=QPoint(0,0);

this->m_KeyRowCount=2;//count from 1 not 0.

this->m_KeyColCount=5;//count from 1 not 0.

}

ZHomeNavigator::~ZHomeNavigator()

{

delete this->m_btnFileManager;

delete this->m_btnSysetmSetup;

delete this->m_btnDateTime;

delete this->m_btnWireless;

delete this->m_btnDevLog;

delete this->m_btnBattery;

delete this->m_btnSound;

delete this->m_btnShutdown;

delete this->m_btnReturn;

delete this->m_gridLayout;

}

void ZHomeNavigator::ZAddWidgetToKeyList(qint32 x,qint32 y,QWidget *widget)

{

ZKeyElement tKeyElement;

tKeyElement.pos=QPoint(x,y);

tKeyElement.element=widget;

this->m_keysList.append(tKeyElement);

}

void ZHomeNavigator::ZSlotShowFileManager()

{

ZFileManager tFileManager;

tFileManager.setGeometry(this->geometry());

tFileManager.exec();

}

void ZHomeNavigator::ZSlotShowSystemSetup()

{

ZSystemSetup tSystemSetup;

tSystemSetup.setGeometry(this->geometry());

//capture delay.

tSystemSetup.ZSetLicensePlateCaptureDelay(gGlobalParam.licencePlateCaptureDelay);

tSystemSetup.ZSetGoodsVideoCaptureDelay(gGlobalParam.goodsVideoCaptureDelay);

//tcp server.

tSystemSetup.ZSetTcpServerIPAddr(gGlobalParam.tcpServerIP);

tSystemSetup.ZSetTcpServerPort(gGlobalParam.tcpServerPort);

if(tSystemSetup.exec()==QDialog::Accepted)

{

//update the delay parameter.

gGlobalParam.licencePlateCaptureDelay=tSystemSetup.ZGetLicensePlateCaptureDelay();

gGlobalParam.goodsVideoCaptureDelay=tSystemSetup.ZGetGoodsVideoCaptureDelay();

//tcp server.

gGlobalParam.tcpServerIP=tSystemSetup.ZGetTcpServerIPAddr();

gGlobalParam.tcpServerPort=tSystemSetup.ZGetTcpServerPort();

}

}

void ZHomeNavigator::ZSlotShowDateTimeSetup()

{

ZDateTimeSetup tDateTimeSetup;

tDateTimeSetup.setGeometry(this->geometry());

tDateTimeSetup.exec();

}

void ZHomeNavigator::keyPressEvent(QKeyEvent *event)

{

switch(event->key())

{

case Qt::Key_Up:

if(this->m_currentKey.x()>0)

{

this->m_currentKey.setX(this->m_currentKey.x()-1);

}

break;

case Qt::Key_Down:

if(this->m_currentKey.x()<this->m_KeyRowCount-1)

{

this->m_currentKey.setX(this->m_currentKey.x()+1);

}

break;

case Qt::Key_Left:

if(this->m_currentKey.y()>0)

{

this->m_currentKey.setY(this->m_currentKey.y()-1);

}

break;

case Qt::Key_Right:

if(this->m_currentKey.y()<this->m_KeyColCount-1)

{

this->m_currentKey.setY(this->m_currentKey.y()+1);

}

break;

}

//find the specified widget.

for(qint32 i=0;i<this->m_keysList.count();i++)

{

ZKeyElement tKeyElement=this->m_keysList.at(i);

if(tKeyElement.pos==this->m_currentKey)

{

tKeyElement.element->setFocus();

break;

}

}

}

void ZHomeNavigator::keyReleaseEvent(QKeyEvent *event)

{

qDebug()<<"release";

}

















某个部件具有焦点时,使用QToolButton:focus来进行qss美化工作。

app.setStyleSheet("QToolButton:focus{background-color:green;}");

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: