1

I'm pretty new to QT's graphic view frame, and I couldn't find anything about this in the docs or on Google.

I have a GUI application that draws a representation for some data. The application itself does some work with matrices / vectors (a neural net thing) and has to represent it on a QGraphicsScene. So far so good, but I've noticed that the app segfaults & crashes sooner or later (and usually sooner) if I try to update the QGraphicsScene from another thread. The QT Docs say nothing about thread-safety & Google gives nothing. What I want (and pretty much need) to do is run the calculations & update the GUI representation accordingly, but the GUI controls etc themself have to remain responsive. As I said, my first thought was to do the whole thing in another thread, but it crashes randomly if I try to.

Is there any "accepted practice" to do this kind of thing in QT or is there some gotcha that I don't know of in the graphics view framework itself?

TC1
  • 1
  • 3
  • 20
  • 31

1 Answers1

2

The Qt docs actually say quite a lot about thread safety. If the docs for QGraphicsScene don't say anything it's because they are not thread-safe, consistent with the behaviour you are seeing.

What you need to do is run your calculations in another thread and synchronise that thread with the main GUI thread as appropriate. A simple way to do this would be to set a flag in the main thread when the calculations are ready for display. That way you can call the appropriate QGraphicsScene methods in the main thread at the right time by simply checking the flag.

Troubadour
  • 13,334
  • 2
  • 38
  • 57
  • Oh, didn't know the QT docs are that way, sorry. Kindof used to javadocs, they usually had it explicitly stated. Anyway, do you know of a way around this then? – TC1 Aug 09 '11 at 19:24
  • I updated my answer. You have to synchronise between the two threads. The way you are doing it now you could be calling a `QGraphicsScene` method in one thread whilst the same `QGraphicsScene` instance is in the middle of executing the same or another of its methods in the other thread. Think of the possible carnage! – Troubadour Aug 09 '11 at 19:29