当前位置:网站首页>利用QEventLoop实现同步等待槽函数返回
利用QEventLoop实现同步等待槽函数返回
2022-07-01 18:55:00 【丶西柚丶】
在做一个项目中碰到一个问题,系统外接了一个IO设备,模块需要触发读取,
QTcpSocket的读取是通过槽函数实现的,模块希望只通过一个读取接口直接得到结果。接口中需要进行一次IO,但是readyRead_Slot不是同步的,这里也不能通过阻塞(尝试了线程join的方式)进行等待,会影响信号和槽的消息队列,未达到预期效果。
测试通过qeventloop手动开启消息循环可以达到这一目标需求。上代码。
void rfidClient::getCurrentRFID(int &result)
{
if(tcpSocket == NULL)
return;
char sbuf[7] = {0};
sbuf[0] = 0xA0;
sbuf[1] = 0x04;
sbuf[2] = 0x01;
sbuf[3] = 0x89;
sbuf[4] = 0x01;
sbuf[5] = 0xD1;
sbuf[6] = '\0';
tcpSocket->write(sbuf);
QEventLoop* el = new QEventLoop();
connect(this, &rfidClient::readFinished, el, &QEventLoop::quit); //IO结束时,结束事件循环
connect(this, &rfidClient::readFinished, this, &rfidClient::analyData); //IO结束时,处理返回结果
el->exec(); //开始事件循环,等待IO结束
// 此时analyData已经处理好
if(rfidResult.size() > 0)
{
result = rfidResult.at(0);
}
else
{
result = -1;
}
边栏推荐
- 关于一个神奇函数的用法
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- Set object value changes null value object
- [research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
- SQL 入门计划-1-选择
- qobject_ Cast usage
- Image acquisition and playback of coaxpress high speed camera based on pxie interface
- 关于new Set( )还有哪些是你不知道的
- Difference between redo and undo
- ORA-01950
猜你喜欢
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
servlet知识点
Interesting! Database is also serverless!
How to turn off the boot auto start software in win11
math_利用微分算近似值
开发那些事儿:EasyCVR平台添加播放地址鉴权功能
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
Interview questions shared in today's group
Getting started with fastdfs
Process steps of vibrating wire acquisition module for measuring vibrating wire sensor
随机推荐
走进如心小镇,数智化变革连接“未来社区”
Install redis under Linux and configure the environment
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
较真儿学源码系列-InheritableThreadLocal(逐行源码带你分析作者思路)
ModSim基本使用(Modbus模拟器)
Arduino Stepper库驱动28BYJ-48步进电机测试程序
Redis installation and startup in Windows environment (background startup)
Object creation
【无标题】
Interview question 1
2022/5/23-2022/5/30
Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant
[untitled]
关于一个神奇函数的用法
[research materials] national second-hand housing market monthly report January 2022 - Download attached
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Time series analysis using kibana timelion
Use of common built-in classes of JS
Oracle 死锁测试
Related concepts of cookies and sessions