0

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.

Qt Newbie
  • 1
  • 1
  • 2
    check out - http://stackoverflow.com/questions/4093159/what-is-the-correct-way-to-implement-a-qthread-example-please this shows how to create a thread and links to the proper docs – g19fanatic Apr 13 '11 at 18:21
  • This question is way too broad. It's as if you are asking for both the QThread documentation and an overview on what threads are and when and how to use them. – Judge Maygarden Apr 13 '11 at 18:47

1 Answers1

2

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

g19fanatic
  • 10,567
  • 6
  • 33
  • 63
Derick Schoonbee
  • 2,971
  • 1
  • 23
  • 39
  • That video is quite good (and relatively recent) and demonstrates how to correctly use QThread with subclasses of QObject (not QThread subclasses). It seems to cover the other multithreading options well too. Your first web page link (although out of date) hints to the correct approach for QThread. Even though the second and third links are to the current documentation, that documentation is still incorrect and suggest subclassing QThread. – Arnold Spence Apr 13 '11 at 19:40
  • Eh eh, the last two links are for completeness... was considering marking them the "old way" but was not sure it would be the correct way to go... so I've edited and removed them - thanks. – Derick Schoonbee Apr 13 '11 at 19:49
  • +1 for including the video. I've been playing with QThreads for a few months, but just half way through the video I learned that I was making using them harder than it had to be. – jwernerny Apr 14 '11 at 13:12
  • @DerickSchoonbee, your video link is broken. – g19fanatic Nov 14 '12 at 13:31