I'm trying to achieve something similar to this. Specifically, I have a queue which gets filled with data, and I want to plot this data in real-time. I currently having a working version which uses a QTimer (and no threads) in order to call a function every half a second. This function takes items out of the queue (while it's nonempty) and plots them. While this certainly works, I was wondering if there's a better way which I should be using (involving threads and the like). My main concern is that I'm scared my approach, while works, could make things more laggy than they need to be. I tried the approach given above, but (if I'm not messing something up, which I probably am):
- terminating the thread was problematic (specifically: "QObject::killTimer: Timers cannot be stopped from another thread")
- The process did not even seem to occur at a different thread, as indicated by the last comment (adding a sleep call in the other thread freezes the entire GUI).
The point is, I was wondering if there's a "better" way to do this than my current approach (such as the one given above, if I can get it to work correctly in my case, or having a thread which checks the queue constantly, and whenever its nonempty, emits a value rather than having a timer).
I'm not giving a minimal reproducible example since this is not specific problem with a particular bit of code necessarily and more so a question about the right approach.
Edit: based on this, would it be best to simply process only one event from the queue at a time and set the timer's timeout to 0ms (rather than looping until the queue isn't empty every half a second)?