QT 编程中使用 QThread 类的帮助,如何使用它
所以我意识到我上次的解释不太清楚,所以我会再试一次。
我有一个程序,它有一个传感器类,每 40 毫秒左右从 Lego NXT 传感器获取一次值。我还有另一个绘制类(我相信在同一个线程中运行),它在虚拟世界中使用 opengl 每 1 毫秒绘制和移动汽车。现在的问题是,每当我的传感器获取信息时,汽车就会非常滞后并且移动缓慢,但是当传感器关闭并且无法获取和输入数据时,汽车就会平稳运行。我该如何解决这个问题,我相信我需要创建另一个线程,但我不知道该怎么做?
注意:传感器与汽车无关。
So I realize my explanantion wasn't too clear last time so I will try again.
I have a program that has a sensor class that gets values from the Lego NXT sensor every 40 miliseconds or so. I also have another draw class (i believe running in the same thread) that draws and moves cars every 1 milisecond using opengl in a virtual world. Now the problem is whenever my sensors are getting information the cars are very laggy and move slowly, but when the sensors are off and not getting and input data the cars run smoothly. How do I fix this problem, I believe i need to create another thread but im not sure how to do that?
note: The sensors have nothing to do with the cars.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给出线程的通用答案并不容易。 Qt 为您提供了各种类来实现线程,因此最好看一下示例。一般来说,您不需要使用 Qt 进行线程化,除非您有一个阻塞 api,我猜您的传感器模块/类就是这样。
这是一个精彩视频现场展示如何创建在另一个线程上运行的对象。您可以尝试将演示中的 LenghtyOperation 实现为您的类,从传感器读取数据,然后发出信号来更新 GUI 线程。
这是另一个链接:没有头痛的线程< /a>
It's not so easy to give a generic answer to threading. Qt gives you various classes to implement threading so it's good to take a look at examples. In general you do not need threading with Qt except if you have a blocking api which I guess your sensor module/class is.
Here is a nice video showing live how to create an object that runs on another thread. You can try to implement the
LenghtyOperation
in the demo as your class that reads data from your sensors and then emit signals to update your GUI thread.Here is another link: Threading without the headache