QT UI multi-thread issue.

  • Hi all ,
    I have a multi-thread process , and use 1 thread to process QT UI . Then I use another thread to render OpenGL Texture .
    Here is a question , sometimes 2 OpenGL(QT UI and OpenGL Texture) will conflict cause broken textures.

    like attaches.
    this is normal UI ,
    it broken textures when conflict happens.

    can someone give an answer to fix this issue , or a direction to survey , or go to other Forum.

    Thanks all.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should add the version of Qt you are using, the platform you are on and possibly the relevant code sources. It's impossible to give any good answers without knowing what you are currently doing.

  • Moderators

    @TL_Tsai Are you properly protecting memory shared between threads? If you are potentially writing at the same time that will cause issues, usually a crash.

    Make sure you protect your shared memory via mutexes like QMutex.

  • @SGaist
    My QT version is 5.9.1 , on Windows and with OpenVR library .
    But about detail codes , I can't tell too much because company's policy.
    Maybe I can show code about how I implement QT in or UI , but need times.

    still thanks your replying.

  • @SGaist
    hello , there is some codes ,
    I declare QOpenGLContext and QQuickRenderControl and QOpenGLFramebufferObject in my header file
    as below
    QOpenGLContext *qOpenGLContext_;
    QOpenGLFramebufferObject *qOpenglFramebufferObject;
    QQuickRenderControl *qQuickRenderControl_

    and use a timer to call render() periodically

    GLuint render () {
    GLuint glTexture = qOpenglFramebufferObject->texture();
    return glTexture;

    How do I improve my code? add mutex or...

    thanks for your watching.

  • Lifetime Qt Champion

    That's a bit not enough, you wrote about multiple threads yet here we only have a really small part that might not reflect at all the complexity of your application.

  • Moderators

    @TL_Tsai From what I see you have shared memory with no mutexing there. qOpenGlContext_, qQuickRenderControl_, and qOpenglFramebufferObject should probably all be mutexed if they are shared between threads.

    Check out QMutex for an easy to use Qt mutex class. You can also use pure c++ mutexes if you'd like. Personally I would go with QMutex since you are already using Qt and it's better. ;)

  • @ambershark
    My situation is one thread create QOpenGLContext , and one thread create native OpenGLContext .
    QOpenGLContext use QTs API as I posted , and OpenGLContext use opengl APIs like glGenTextue , glTexImage2d.

    I'm not sure use mutex can fix this situation or there is conflict between QtOpenGL and native OpenGL originally.

  • Moderators

    @TL_Tsai I had assumed they were created in a single thread and shared.. hmm.

    I'm not really sure how to help you. Threading issues are the most complicated to solve. It's difficult with knowledge of the project and source code. It's almost impossible without at least source code to look at.

    I wish I had more advice but the information is just too limited. Good luck though, I'm sure you'll find it. :)

  • Lifetime Qt Champion

    Why are you mixing both ?

Log in to reply

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