2
votes

I created an application with a Qt GUI that does image processing. For one part of the computations, OpenGL is used to render a transformed image (off-screen). Everything worked fine. However, I did all the heavy processing when a button was clicked in the GUI thread. Since computations take several seconds, the GUI then became non-responsive.

Fortunately Qt already includes a Thread libray (QThread, QtConcurrent etc.). Now I just use QtConcurrent::run() and a QFutureWatcher<cv::Mat> to do the computations asynchronously with a callback function. The problem is that during the computations, the OpenGL function is called multiple times and can now run on different threads for consecutive button clicks. If the rendering is done on a different thread than on the one OpenGL was initialized, OpenGL renders nothing (black screen). Of course this depends on the thread handling of Qt.

So my question is: is there an easy solution using the current setup (Qt threads and OpenGL) without additional libraries etc. to enforce the rendering to be run always on the same thread.

I know that there's a method QObject::moveToThread(), but I don't know exactly how to use it in this case. The OpenGL stuff does not contain any Qt-related code or functions at all. So how could I solve this problem?

And I also couldn't figure out if a certain QThread always uses the same underlying native thread. Is this the case?

1

1 Answers

2
votes

Here is one approach. You create a thread which lives as long as you need and you add jobs to it. You could replace QThread or use std::thread.