我做了一个读卡工具,有个连续读卡功能,读卡时还得一直寻卡,寻到卡后才能读;为避免死等,可以在连续读卡中去停止读卡,所以需要写一个线程用来读卡;
我使用的是moveToThread方式;
我有个窗口程序,有个读卡类;我希望读卡时是个线程在操作,所以我在窗口上定义了一个线程对象:
//在类中定义一个QThread对象m_thread
QThread m_thread;`
并把读卡器类设置到线程上并启动线程:
// 启动读卡线程
readIdCard.moveToThread(&m_thread);
m_thread.start();
读卡器类中,新增一个定时器,并且设置定时器超时就掉一下读卡接口readCardOnce;
timer = new QTimer(this);
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), this, SLOT(readCardOnce()));
readCardOnce接口:
if(!m_isRead){
// 设此标记是为了能即使停定时器
traceLogLib.outPutMsg(QtDebugMsg, "ReadIdCard::readerCardOnce invoke end. m_isRead = false");
return