Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLWidget update and grab in thread
Qt 6.12 LTS is out! See what's new in the release blog

QOpenGLWidget update and grab in thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 1.1k 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.
  • M Offline
    M Offline
    MfromOlsztyn
    wrote on last edited by
    #1

    How to do it correctly?

    1. Create Thread
    2. Create in thread object based on QOpenGLWidget
    3. in Thread call grab() and take correct picture

    I open example hellogl2 and add this sample code in main.cpp before return app.exec();

        QShortcut *shortcutF4 = new QShortcut(QKeySequence(Qt::Key_F4), &mainWindow, nullptr, nullptr, Qt::ApplicationShortcut);
        QObject::connect(shortcutF4, &QShortcut::activated, &mainWindow, [&]()
        {
            qDebug() << "F4";
            auto *t = new std::thread([]()
            {
                 GLWidget glWidget;
                 glWidget.resize(400,400);
                 glWidget.grab().save("grab_hellogl2_in_thread_01.png");
                 // change something
                 glWidget.setXRotation(60*3);
                 glWidget.grab().save("grab_hellogl2_in_thread_02.png");
                 // change something
                 glWidget.setYRotation(75*4);
                 glWidget.grab().save("grab_hellogl2_in_thread_03.png");
    
            });
            t->join();
        });
    
        return app.exec();
    

    But I see warning "Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures."

    How to do it correctly?

    O 1 Reply Last reply
    0
    • M MfromOlsztyn

      How to do it correctly?

      1. Create Thread
      2. Create in thread object based on QOpenGLWidget
      3. in Thread call grab() and take correct picture

      I open example hellogl2 and add this sample code in main.cpp before return app.exec();

          QShortcut *shortcutF4 = new QShortcut(QKeySequence(Qt::Key_F4), &mainWindow, nullptr, nullptr, Qt::ApplicationShortcut);
          QObject::connect(shortcutF4, &QShortcut::activated, &mainWindow, [&]()
          {
              qDebug() << "F4";
              auto *t = new std::thread([]()
              {
                   GLWidget glWidget;
                   glWidget.resize(400,400);
                   glWidget.grab().save("grab_hellogl2_in_thread_01.png");
                   // change something
                   glWidget.setXRotation(60*3);
                   glWidget.grab().save("grab_hellogl2_in_thread_02.png");
                   // change something
                   glWidget.setYRotation(75*4);
                   glWidget.grab().save("grab_hellogl2_in_thread_03.png");
      
              });
              t->join();
          });
      
          return app.exec();
      

      But I see warning "Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures."

      How to do it correctly?

      O Offline
      O Offline
      ollarch
      wrote on last edited by
      #2

      @MfromOlsztyn All the GUI stuff must be done into main thread.

      M 1 Reply Last reply
      1
      • O ollarch

        @MfromOlsztyn All the GUI stuff must be done into main thread.

        M Offline
        M Offline
        MfromOlsztyn
        wrote on last edited by MfromOlsztyn
        #3

        @ollarch
        I don't want to show anything in the GUI

        I want render off-screen, it is possible in openGL.

        Christian EhrlicherC W 2 Replies Last reply
        0
        • M MfromOlsztyn

          @ollarch
          I don't want to show anything in the GUI

          I want render off-screen, it is possible in openGL.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MfromOlsztyn said in QOpenGLWidget update and grab in thread:

          I don't want to show anything in the GUI

          But GLWidget is a widget and therefore needs to be created and access in the main thread as corectly told you already by @ollarch .

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • M MfromOlsztyn

            @ollarch
            I don't want to show anything in the GUI

            I want render off-screen, it is possible in openGL.

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

            @MfromOlsztyn You can't touch a widget from a secondary thread. The goal just isn't correct. There is no way to do that correctly. Widgets are always "GUI" even if it isn't shown.

            Threaded OpenGL is potentially a complex topic, but you'll need an offscreen surface that isn't a widget.

            M 1 Reply Last reply
            0
            • W wrosecrans

              @MfromOlsztyn You can't touch a widget from a secondary thread. The goal just isn't correct. There is no way to do that correctly. Widgets are always "GUI" even if it isn't shown.

              Threaded OpenGL is potentially a complex topic, but you'll need an offscreen surface that isn't a widget.

              M Offline
              M Offline
              MfromOlsztyn
              wrote on last edited by MfromOlsztyn
              #6

              @wrosecrans I look at https://doc.qt.io/qt-6/qopenglwidget.html#threading

              *Performing offscreen rendering on worker threads, 
              for example to generate textures that are then used 
              in the GUI/main thread in paintGL(), are supported by exposing 
              the widget's QOpenGLContext so that additional contexts 
              sharing with it can be created on each thread.*
              

              I don't want to touch main from background thread ,
              and I don't want to touch background thread from main.
              Two independent threads, no interaction

              Another way: create second process without GUI and use QOpenGLWidget in invisible APP, is it possible ?

              I'm looking for a starting example
              A lot of people ask, but I don't see a solution

              Chris KawaC 1 Reply Last reply
              0
              • M MfromOlsztyn

                @wrosecrans I look at https://doc.qt.io/qt-6/qopenglwidget.html#threading

                *Performing offscreen rendering on worker threads, 
                for example to generate textures that are then used 
                in the GUI/main thread in paintGL(), are supported by exposing 
                the widget's QOpenGLContext so that additional contexts 
                sharing with it can be created on each thread.*
                

                I don't want to touch main from background thread ,
                and I don't want to touch background thread from main.
                Two independent threads, no interaction

                Another way: create second process without GUI and use QOpenGLWidget in invisible APP, is it possible ?

                I'm looking for a starting example
                A lot of people ask, but I don't see a solution

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MfromOlsztyn As others said you can't render to a widget in any thread but main. For rendering in another thread use QOffscreenSurface instead of QOpenGLWidget, which creates a windowless surface and is specifically designed for this purpose.

                1 Reply Last reply
                1

                • Login

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