简体   繁体   中英

QOpenGLWidget and multithreading

I am developing a 3D app using Qt and OpenGL. The app is composed of a QMainWindow with a QOpenGLWidget as central widget and a QML UI as a dock widget. I realized that the user inputs and the UI depends on the rendering performance: if my app runs with low fps, the user inputs are not all caught and it gets difficult to use the UI.

So I was thinking about doing the rendering in a separate thread. I tried several techniques, like using a QTimer or a QThread, but I always get problems sharing the OpenGL context, resizing or using a QPainter.

I am wondering if doing the rendering in another thread is a good approach.

Any suggestions, advices ?

Thanks.

Typical GUI frameworks are not designed to be used from multiple threads directly, and QT is not an exception from. Trying to do GUI stuff from different threads typically results in problems of some kind.

Those frameworks normally have an internal event queue where events are placed in and then processed one after another, which, if the framework is used correctly, assures that the GUI related stuff is accessed from one single thread only. But they allow to add additional events into the queue.

And here we are at the way to go: Keep the entire GUI in one single thread and do user input processing in the other thread. As soon as user data is processed, feed your GUI with appropriately.

Ways to do so offered by Qt are eg invoke function or the event system .

Just don't use QOpenGLWidget . Use a single QML window for everything.

Render your OpenGL things in pre-render or post-render function of the QML by using the QQuickWindow::beforeRendering() or QQuickWindow::afterRendering() signals.

That will be using the rendering thread of the QML, so you won't need to create it. And the use cases and synchronization are explained in the qt docs:

http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM