Create raw GL context in Qt?

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?

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();

Need Your Help

Using new on a function that returns some value in javascript?

javascript

I was experimenting with inheritance and creating objects and somehow this confused me.

g++ regex crash on (possibly unsyntactic) expression

c++ regex c++11 crash g++4.8

I figure the following program should either complain it can't compile the regular expression or else treat it as legal and compile it fine (I don't have the standard so I can't say for sure whethe...