Best way to handle drawing from thread



  • I have a thread running. What I want to do, is draw some images in my QWindow depending on what's going on in this thread.

    So I put QGraphicsView in my QWindow, QScene inside of that QGraphicsView and finally I added some QGraphicsObjects to the scene. This part works nicely.

    Now, in my thread, I do some math and then trying to run something like object1->setTransform(), where object1 is pointer to QGraphiscObject that was added to QScene. (No semaphores or other forms of memory protection for now.) And it kinda works, but I keep getting QObject::startTimer: Timers cannot be started from another thread warning during the runtime. On the top of that, transformed object is flickery sometimes (I change it at least 30 times/s).

    My question is: What is the proper way to do that? Is QGraphicsView right tool for the job? I'm thinking of adding OpenGl in the future. How does it compute?


  • Moderators

    @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.



  • @jsulm said in Best way to handle drawing from thread:

    @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.

    Purely out of idle/malicious curiosity --- I have no usage case --- but would this still apply if you suspended the UI thread while drawing from the other thread (then exited that thread and restarted UI thread)? i.e. is the reason contention issues, or does the UI thread have local stuff which is needed but only it can access? I imagine the latter (as well as the former, I suppose), but just interested to hear.


  • Moderators

    @JNBarchan I don't know and would never do something like this.



  • @jsulm Lol, OK, I was just interested :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.