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. How to move to thread opengl widget that was promoted in QtDesigner?
Forum Updated to NodeBB v4.3 + New Features

How to move to thread opengl widget that was promoted in QtDesigner?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.3k 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.
  • N Offline
    N Offline
    never_ever
    wrote on last edited by
    #1

    Hi,
    I have two different widgets (for example aGLWidget and bGLWidget). They inherit QOpenGLWidget. I also created QWidget (myGLWindow) that contains QTabWidget. There is two tabs, each contain one widget promoted to one of my QOpenGLWidget. I need to move that section to different thread, because I have also view from camera in other window. So it should works asynchronously.
    I know that I can't move myGLWindow because it is GUI object created by the main window. So please tell how can I run that two QOpenGLWidget in other thread if I create them by promoting?

    kshegunovK 1 Reply Last reply
    0
    • N never_ever

      Hi,
      I have two different widgets (for example aGLWidget and bGLWidget). They inherit QOpenGLWidget. I also created QWidget (myGLWindow) that contains QTabWidget. There is two tabs, each contain one widget promoted to one of my QOpenGLWidget. I need to move that section to different thread, because I have also view from camera in other window. So it should works asynchronously.
      I know that I can't move myGLWindow because it is GUI object created by the main window. So please tell how can I run that two QOpenGLWidget in other thread if I create them by promoting?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @never_ever
      Hello,
      Promoting has nothing to do with the issue. Widgets (QOpenGLWidget included) are not reentrant, so you can't, or rather mustn't, move them into a thread different from the main.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        never_ever
        wrote on last edited by never_ever
        #3

        Ok, I read more about QOpenGLWdget and I found that I can move context to the thread, but I have to also reimplement paintEvent to do nothing.
        So I think I should do this:

        QThread* glThread = new QThread();
        aGLWidget->context()->moveToThread(glThread);
        bGLWidget->context()->moveToThread(glThread);
        

        And then, after thread is done rendering I should update my GUI, but how do I know that rendering is done? If I should cubclass my own thread or there is some signal for this?

        What with resizeEvent? If I also should reimplement it to do nothing?

        kshegunovK 1 Reply Last reply
        0
        • N never_ever

          Ok, I read more about QOpenGLWdget and I found that I can move context to the thread, but I have to also reimplement paintEvent to do nothing.
          So I think I should do this:

          QThread* glThread = new QThread();
          aGLWidget->context()->moveToThread(glThread);
          bGLWidget->context()->moveToThread(glThread);
          

          And then, after thread is done rendering I should update my GUI, but how do I know that rendering is done? If I should cubclass my own thread or there is some signal for this?

          What with resizeEvent? If I also should reimplement it to do nothing?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @never_ever
          Hello,
          Only one thread is able to draw onto a context, or rather the surface of a given context, note the documentation's warning about thread affinity. That said, somewhere from the main thread you should call swapBuffers to make your changes visible, this entails managing the double-buffering yourself. You should know when rendering is done, since you do that in your worker threads, so you can connect a signal from the worker(s) to the GUI to notify it when you've finished drawing the surface. You don't need to subclass the QThread class, instead extend QObject and move an instance of it to the worker thread and use it to do the rendering/other work for you (the so called worker object approach). I'm pretty sure that if you need to resize the window (like handling the resize event) at some point, you'll need to add a mutex to serialize access to your context, so when resizing is done there will be no rendering occurring at the same time from another thread, that you can do relatively simply with a single QMutex object. I hope this helps.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          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