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.