Qt5.5中使用serialport类使得Ui界面很卡
2015-11-24 19:24
459 查看
实际的读取串口工作中出现了这种情况,经过分析问题,可能有以下几个原因。
第一 ,在timer(out)信号触发的readData事件内,出现了非常复杂的循环,程序逻辑问题带来的时间复杂度的增加。
第二,readdata的数据量过大,也就是缓冲去buffer的长度太大了,每个循环中读入的实际数据量远小于maxsize,因此程序就会在forever循环中不停的循环读入数据装入buffersize直到其装满为止。
第三,最容易引起Ui界面卡的原因,就是设置的串口Comm的waitforreadyread(timeout)过大,以至于read动作一直在timeout时间内等待数据的到来,程序处理在这里暂停了timeout时间,这个timeout时间如果超过0.5s,Ui界面自然就卡的不行。
当然,使用waitforreadyread使得Ui变得非常卡的原因就是这个函数阻塞了当前线程。
以下是Qt QIODevice类中有关于这个函数的文档:
bool QIODevice::waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out.
Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to the readyRead() signal, readyRead() will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
第一 ,在timer(out)信号触发的readData事件内,出现了非常复杂的循环,程序逻辑问题带来的时间复杂度的增加。
第二,readdata的数据量过大,也就是缓冲去buffer的长度太大了,每个循环中读入的实际数据量远小于maxsize,因此程序就会在forever循环中不停的循环读入数据装入buffersize直到其装满为止。
第三,最容易引起Ui界面卡的原因,就是设置的串口Comm的waitforreadyread(timeout)过大,以至于read动作一直在timeout时间内等待数据的到来,程序处理在这里暂停了timeout时间,这个timeout时间如果超过0.5s,Ui界面自然就卡的不行。
当然,使用waitforreadyread使得Ui变得非常卡的原因就是这个函数阻塞了当前线程。
以下是Qt QIODevice类中有关于这个函数的文档:
bool QIODevice::waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out.
Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to the readyRead() signal, readyRead() will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
相关文章推荐
- Handler、Message、MessageQueue、Looper调用过程源码浅析
- 10.26~11.23刷题(shui)记录
- System.Web.UI.ViewStateException: Invalid viewstate 解决方法
- 实现UITableView上View的上拉隐藏下拉出现效果(1)(category)
- invalid use of incomplete type 'class-Ui-Widge't的解决办法
- Tui-x 自适应屏幕 (转) ----- 6
- Tui-x 运行demo和部署项目 ( 转 ) ----- 5
- Tui-x 资源管理( 转 ) ----- 4
- Tui-x 命名规则( 转 ) ----- 3
- Tui-x 基础使用 ( 转 ) ----- 2
- Tui-x简介 ( 转 ) ---- 1
- UIScrollView的属性总结
- easyui-datagrid---动态创建表头和加载数据
- UIimage存入本地并读取
- UIPickerView(API+选择器使用)
- 百度编辑器UEditor使用
- easyui-datagrid 编辑模式详解
- POJ 2299 Ultra-QuickSort 【树状数组求逆序数】
- html required属性
- UIView 设置圆角和边框备忘