当前位置:网站首页>利用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;
}边栏推荐
- Solve the problem of slow or failed vscode download
- 开发那些事儿:EasyCVR平台添加播放地址鉴权功能
- 墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
- DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
- Sum the amount
- [exercise] HashSet
- 【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
- 基于图的 Affinity Propagation 聚类计算公式详解和代码示例
- 由浅入深学会白盒测试用例设计
- MySQL reports an error can't create table 'demo01 tb_ Student‘ (errno: 150)*
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

How to turn off the boot auto start software in win11

servlet知识点

牛客编程题--必刷101之字符串(高效刷题,举一反三)

GC garbage collection

基于图的 Affinity Propagation 聚类计算公式详解和代码示例

Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant

Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.

实例讲解将Graph Explorer搬上JupyterLab

独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
随机推荐
Install redis under Linux and configure the environment
自定义插入页面标签以及实现类似通讯录的首字母搜索
DS transunet: Dual Swing transformer u-net for medical image segmentation
1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包
How can a programmer grow rapidly
使用 Kibana Timelion 进行时间序列分析
#yyds干货盘点#SQL聚合查询方法总结
MySQL reports an error can't create table 'demo01 tb_ Student‘ (errno: 150)*
3D全景模型展示可视化技术演示
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Bind this of the current scope for callback functions in other cases such as timers and delayers
Iframe 父子页面通信
再回顾集合容器
Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
List is divided into sets that meet and do not meet conditions (partitioningby)
How to add transactions in JDBC
数据分析师听起来很高大上?了解这几点你再决定是否转型
Redis installation and startup in Windows environment (background startup)
一文读懂C语言中的结构体