PDA

View Full Version : Updating the transform of QGrapicsItems in a separate thread



Cruz
9th June 2017, 12:04
Hi!

I'm developing a little 2D top down car driving game using the Qt graphics view framework in combination with the Box2D physics simulation engine. Everything in my game is a polygon, so I derived all my objects from QPolygonItem, which inherits from QGraphicsItem. After construction, I call an init() method where all my objects are added to a QGraphicsScene and to the physical simulation. Then, in the main control loop, I step the simulation and update the transform of all my objects using setPos() and setRotation(). The scene should then automatically visualize the motion of the objects. This works nicely when I step the main control loop manually as in calling its step() function by the press of a button, which is probably handeled by the main thread or the gui thread, the same thread the scene is updated in? However, when I start the main control loop, which is a separate thread, I see this console output:


QObject::startTimer: Timers cannot be started from another thread

and the one polygon I'm testing this with disappears. I found out that the output is produced when calling setPos() on the item, clearly in an other thread than perhaps intended by the internal design of the graphics view framework. I wonder what's happening in the background here and what timer needs to be started when I just want to update a transform, but in any case, what can I do to solve this please?