bool GiveBackCard2::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() == QEvent::KeyPress)
{
QKeyEvent event = static_cast<QKeyEvent>(ev);
int iKey = event->key();
if(iKey == Qt::Key_Return || iKey == Qt::Key_Enter) //回车发送存储信息
{
if(!_outerCardID.isEmpty())
{
if(_outerCardID==_cardID)
{
_canGiveBack=true;
setTipLabel(QString::fromLocal8Bit(“匹配成功”));
}
else
{
_canGiveBack=false;
setTipLabel(QString::fromLocal8Bit(“匹配失败”));
}
}
return true;
}
else if(0 <= iKey && 128 > iKey) //可显示字符键值
{
//判断距离上次有效按键时间间隔,大于200ms重置输入存储,否则累加记录字符
int iElapsed = m_tmWatch.msecsTo(QTime::currentTime());
//qDebug() << iElapsed << iKey;
if(200 < iElapsed)
{
_outerCardID = event->text();
}
else
{
_outerCardID += event->text();
}
m_tmWatch = QTime::currentTime();
}
}
return false;
}
当带有Qt::Tool时,窗口默认是无焦点的,需要用代码进行控制激活。
this->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);
注意,读卡器输入需要人工激活窗口界面:
int GiveBackCard2::exec()
{
show();
activateWindow();
return QDialog::exec();
}
qt检测读卡器模拟键盘输入
最新推荐文章于 2024-02-29 20:41:35 发布