Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWindow on a non-GUI thread
Forum Updated to NodeBB v4.3 + New Features

QWindow on a non-GUI thread

Scheduled Pinned Locked Moved Solved General and Desktop
qwindowopenglthreaded
11 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    IIRC, you can move your QOpenGLContext to a different thread but not the QWindow.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    2
    • SGaistS SGaist

      Hi,

      IIRC, you can move your QOpenGLContext to a different thread but not the QWindow.

      R Offline
      R Offline
      rtavakko
      wrote on last edited by
      #3

      @SGaist Thanks for replying.

      Are you aware of any alternatives to QWindow where using a thread is possible?

      I do most of my rendering on a QOffscreenSurface subclass and the QWindow is only displaying the result. But since it sits on the GUI thread, any events there cause it to drop frames and stall.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Maybe the QQuickRenderControl example might help.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply
        1
        • SGaistS SGaist

          Maybe the QQuickRenderControl example might help.

          R Offline
          R Offline
          rtavakko
          wrote on last edited by
          #5

          @SGaist Thanks! I will play around with that example

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rtavakko
            wrote on last edited by
            #6

            I'm still stuck on this.

            Anyone know if there is a way to use a QOpenGLContext and other QT OpenGL resources with a native window?

            QSurface seems to fit this purpose and it seems to be compatible with non-GUI thread use. I'm assuming I have to implement window resizing and visibility but I'm not sure where to start with that. If anyone's played around with that concept, do you have any tips or advice on getting started?

            W 1 Reply Last reply
            0
            • R Offline
              R Offline
              rtavakko
              wrote on last edited by rtavakko
              #7

              An update on this mainly based on the results of this thread (links to example projects are in this thread):
              https://forum.qt.io/topic/125267/wgl-window-on-non-gui-thread

              To summarize things, the real issue here is using QT's OpenGL classes to render into a QWindow or similar class. Because QWindow can only live in the GUI thread, projects that have more complex UI and a lot of widgets, will have a hard time with high resolution and high framerate OpenGL rendering.

              There are two options at this time to have decent OpenGL performance while also using QT's UI:

              1 - Using QT's OpenGL classes and UI while optimizing UI widgets:

              This is the concept that QT is currently designed around and what everyone will recommend. Basically what I personally had to do was to use QT's concurrent programming options. The easiest example:

              QtConcurrent::run([=](){
              //Code that is currently causing your UI to block
              });
              

              While this option works and makes sense, it complicates things. You obviously cannot make UI calls in the threaded block of code and also there is the limitation of not having any control over existing QT implementations. For example, if a certain widget is by default performance-intensive, you are stuck with a performance hit.

              2 - Using QT's UI with non-QT OpenGL classes:

              The only option for rendering into an OpenGL window on a non-GUI thread is to get away completely from QT OpenGL classes and use a library like GLFW.

              I know people on this forum tend to immediately shut down the idea of a window on a non-GUI thread, but this seems to be the best way for complex projects using OpenGL.

              3 (Not possible currently) - Using QT's UI with QT OpenGL classes, along with a QWindow-type class that can actually live in a non-GUI thread:

              This would be heaven. The concept of a QOffscreenSurface being able to render on a non-GUI thread but not being able to do the same for QWindow really makes no sense. I understand that this is done to allow for events to be delivered to a QWindow but it would make sense to create some flexibility for programmers to take care of resizing and visibility themselves since this is perfectly doable with OpenGL.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Hi,

                For internal design and discussions about the OpenGL stack you should go to the interest mailing list. You'll find there Qt's developers/maintainers, this forum is more user oriented.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  For internal design and discussions about the OpenGL stack you should go to the interest mailing list. You'll find there Qt's developers/maintainers, this forum is more user oriented.

                  R Offline
                  R Offline
                  rtavakko
                  wrote on last edited by
                  #9

                  @SGaist Thanks, I will do that. I wanted to also share my experience to best use QT's existing OpenGL and UI features.

                  1 Reply Last reply
                  0
                  • R rtavakko

                    I'm still stuck on this.

                    Anyone know if there is a way to use a QOpenGLContext and other QT OpenGL resources with a native window?

                    QSurface seems to fit this purpose and it seems to be compatible with non-GUI thread use. I'm assuming I have to implement window resizing and visibility but I'm not sure where to start with that. If anyone's played around with that concept, do you have any tips or advice on getting started?

                    W Offline
                    W Offline
                    wrosecrans
                    wrote on last edited by
                    #10

                    @rtavakko said in QWindow on a non-GUI thread:

                    QT OpenGL resources with a native window?

                    Most of the Qt OpenGL helper functions should actually "just work." They use whatever is the "current" OpenGL context. You just can't touch the GUI stuff. Something QOpenGLShaderProgram will have no idea if you are ultimately going to be rendering to a window or an offscreen surface.

                    That said, I ultimately gave up trying to do multithreaded OpenGL with Qt. Between the Qt limitations, and the GL language-lawyering about what exactly is an "object" that will be shared between different contexts, etc. If you do try to do multithreaded OpenGL, you'll probably be much better off with an offscreen rendering thread and just drawing the most current completed FBO in the GUI window.

                    You may want to pick up Vulkan if you really want to go down the rabbit hole of multithreaded drawing. It's a much more modern design, with much more explicit state that can be coordinated between threads and concurrent operations explicitly. (But it is a pain in the ass to do all of that. There's no "easy" GPU drawing in parallel.)

                    R 1 Reply Last reply
                    0
                    • W wrosecrans

                      @rtavakko said in QWindow on a non-GUI thread:

                      QT OpenGL resources with a native window?

                      Most of the Qt OpenGL helper functions should actually "just work." They use whatever is the "current" OpenGL context. You just can't touch the GUI stuff. Something QOpenGLShaderProgram will have no idea if you are ultimately going to be rendering to a window or an offscreen surface.

                      That said, I ultimately gave up trying to do multithreaded OpenGL with Qt. Between the Qt limitations, and the GL language-lawyering about what exactly is an "object" that will be shared between different contexts, etc. If you do try to do multithreaded OpenGL, you'll probably be much better off with an offscreen rendering thread and just drawing the most current completed FBO in the GUI window.

                      You may want to pick up Vulkan if you really want to go down the rabbit hole of multithreaded drawing. It's a much more modern design, with much more explicit state that can be coordinated between threads and concurrent operations explicitly. (But it is a pain in the ass to do all of that. There's no "easy" GPU drawing in parallel.)

                      R Offline
                      R Offline
                      rtavakko
                      wrote on last edited by
                      #11

                      @wrosecrans

                      Yes, I'm rendering using a QOffscreenSurface on a separate thread then displaying the frame on a QWindow on the GUI thread, but it makes sense to have all of the video rendering on the same thread so we're not interfering with the GUI thread.

                      I've successfully tested this concept with GLFW-based OpenGL windows and QT UI. I doubt Vulkan will be any different since from what I understand the render window will still need to sit on the GUI thread.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved