1

I googled and found out, that there is an option to draw on a separate thread using qml. http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html

But it's not what I need. How can I render in a separate thread using common qt widgets without qml?

Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
  • There is an older sample for this http://doc.qt.io/archives/qt-4.8/qt-demos-glhypnotizer-example.html – Eelke Oct 07 '18 at 08:42

1 Answers1

-1

If your QWidget inherits from QOpenglWidget you can just call

 this->makeCurrent();

But I personally prefer a more efficient and robust way to encapsulate OpenGL context by using QWindow and configuring all the OpenGL related settings there:

Here is an example:

bool MyOpenGLWindow::Create()
{
  this->requestActivate();
  if(!glContext)
  {

    glContext= new QOpenGLContext(this);
    QSurfaceFormat fmt = requestedFormat();
    int maj = fmt.majorVersion();
    int min = fmt.minorVersion();
    glContext->setFormat( requestedFormat());
    glContext->create();
    if(glContext->isValid() == false)
    {
        QString str;
        str.sprintf("Failed to create GL:%i.%i context",maj,min);
        return false;
    }
  }

  glContext->makeCurrent(this);
  //after this line you can work with OpenGL
  initializeOpenGLFunctions();
  glDisable(GL_DEPTH_TEST);  
  glEnable(GL_CULL_FACE);
  //etc...

Now,remember, every time you spawn (or move existing one) an instance of such a class in a thread you must set the context current.

Michael IV
  • 11,016
  • 12
  • 92
  • 223