Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create raw GL context in Qt?

Tags:

c++

qt

opengl

I am using Qt for a project. It has some QGLWidgets and these work beautifully.

The problem is, I have some legacy code I want to use that uses raw OpenGL commands to do some texture and mesh processing (render meshes to images, etc). I want to call these functions from within my Qt code, but of course that requires that I set up a fresh OpenGL context before I call the OpenGL commands.

I tried to do the following:

QGLContext context(QGLFormat::defaultFormat());
std::cout << "context creation: " << context.create() << std::endl;

if(!context.isValid())
{
    std::cout << "Cannot create GL context" << std::endl;
    return false;
}

context.makeCurrent();
callLegacyOpenGLCode();

but it doesn't work. QGLContext::create() is returning false. This is on Windows 7 using Qt 4.8, compiled with OpenGL support.

Is this the wrong way to ask Qt to create a new OpenGL context for me? What should I do instead?

like image 837
user168715 Avatar asked Jan 30 '14 04:01

user168715


1 Answers

For anybody else searching about this issue, @ratchetfreak's suggestion is a good one. The following code works correctly:

QGLWidget tmpwidget;

if(!tmpwidget.isValid())
{
    std::cout << "Cannot create GL context" << std::endl;
    return false;
}

tmpwidget.makeCurrent();
callLegacyOpenGLCode();
like image 192
user168715 Avatar answered Sep 18 '22 23:09

user168715